Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Créer un script de démarrage
Cette page explique comment créer un script de démarrage pour le service Web, puis comment le valider.
Pour vous assurer que le service Web ne nécessite pas d'intervention manuelle, vous devez créer un script de démarrage. Le script de démarrage effectue les tâches suivantes :
Il lit les métadonnées de la machine virtuelle (VM) et définit la variable d'environnement pour les métadonnées avec le préfixe CONNECTOR_ENV. Toutes les données requises par les consommateurs sont extraites de Marketplace lors de la création de la VM et définies en tant que variables d'environnement dans Docker. Ces variables d'environnement peuvent ensuite être lues et traitées en conséquence dans l'application.
Il démarre le conteneur Docker dans lequel se trouve le service Web avec les variables d'environnement appropriées.
Le code suivant est un exemple de script de démarrage :
#!/bin/bash# 1. Fetch Metadata Keysmetadata_keys_url="http://metadata.google.internal/computeMetadata/v1/instance/attributes/"metadata_keys=$(curl-H"Metadata-Flavor: Google""$metadata_keys_url")# 2. Set Environment Variables for CONNECTOR_ENV Keys (with error handling)forkeyin$metadata_keys;doif[[$key==CONNECTOR_ENV_*]];thenmetadata_value_url="http://metadata.google.internal/computeMetadata/v1/instance/attributes/$key"# Fetch value with error handlingvalue=$(curl-H"Metadata-Flavor: Google""$metadata_value_url"2>/dev/null)if[[-z"$value"]];thenecho"Warning: No value found for key '$key'. Skipping.">&2# Log the warning to stderrcontinue# Skip to the next iterationfiexport"$key=$value"fidone# 3. Run Docker with Environment Variables
sudodockerstopconnector-service||true
sudodockerrun\--nameconnector-service\$(env|grepCONNECTOR_ENV_|sed's/=/="/;s/$/"/'|sed's/^/-e /')\-d-p$CONNECTOR_ENV_PORT:$CONNECTOR_ENV_PORT\--restart=unless-stopped\connector-container
Valider le script de démarrage
Dans l'instance de VM, ajoutez les métadonnées du port et tous les autres paramètres requis lors de la création de la VM.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/03 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/03 (UTC)."],[[["\u003cp\u003ePre-GA products and features, as described here, are subject to the "Pre-GA Offerings Terms" and are provided "as is" with potential limitations in support.\u003c/p\u003e\n"],["\u003cp\u003eA startup script is crucial for web services to eliminate manual intervention during startup, as the script automatically reads VM metadata and sets environment variables.\u003c/p\u003e\n"],["\u003cp\u003eThe provided sample startup script fetches metadata, sets environment variables prefixed with \u003ccode\u003eCONNECTOR_ENV\u003c/code\u003e, and starts a Docker container with these variables, ensuring the web service runs.\u003c/p\u003e\n"],["\u003cp\u003eTo validate the script, you need to add necessary metadata to the VM, add the startup script to the VM's configuration, and execute the script using the \u003ccode\u003egoogle_metadata_script_runner\u003c/code\u003e to verify docker is running.\u003c/p\u003e\n"],["\u003cp\u003eAfter testing the docker service should be removed from the vm.\u003c/p\u003e\n"]]],[],null,["# Create a startup script\n\n| **Preview**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nCreate a startup script\n=======================\n\nThis page describes how to create a startup script for the web service and then validate the script.\n\nTo ensure that bringing up the web service does not require manual intervention, you must create a startup script.The startup script does the following tasks:\n\n- Reads the virtual machine (VM) metadata and sets the environment variable for metadata with the `CONNECTOR_ENV` prefix. Any data required by consumers is taken during VM creation from Marketplace and is set as environment variables in docker. These environment variables can then be read and processed accordingly in the application.\n- Starts the docker container containing the web service with the appropriate environment variables.\n\nThe following code is a sample startup script: \n\n```bash\n#!/bin/bash\n\n# 1. Fetch Metadata Keys\nmetadata_keys_url=\"http://metadata.google.internal/computeMetadata/v1/instance/attributes/\"\nmetadata_keys=$(curl -H \"Metadata-Flavor: Google\" \"$metadata_keys_url\")\n\n# 2. Set Environment Variables for CONNECTOR_ENV Keys (with error handling)\nfor key in $metadata_keys; do\n if [[ $key == CONNECTOR_ENV_* ]]; then\n metadata_value_url=\"http://metadata.google.internal/computeMetadata/v1/instance/attributes/$key\"\n\n # Fetch value with error handling\n value=$(curl -H \"Metadata-Flavor: Google\" \"$metadata_value_url\" 2\u003e/dev/null)\n if [[ -z \"$value\" ]]; then\n echo \"Warning: No value found for key '$key'. Skipping.\" \u003e&2 # Log the warning to stderr\n continue # Skip to the next iteration\n fi\n export \"$key=$value\"\n fi\ndone\n\n# 3. Run Docker with Environment Variables\nsudo docker stop connector-service || true\nsudo docker run \\\n --name connector-service \\\n $(env | grep CONNECTOR_ENV_ | sed 's/=/=\"/;s/$/\"/' | sed 's/^/-e /') \\\n -d -p $CONNECTOR_ENV_PORT:$CONNECTOR_ENV_PORT \\\n --restart=unless-stopped \\\n connector-container\n```\n\n### Validate the startup script\n\n1. In the VM instance, add the metadata for the port and all other parameters which are required during VM creation. \n\n ```bash\n gcloud compute instances add-metadata VM_NAME \\ \n --zone=VM_ZONE \\\n --project=PROJECT_NAME \\\n --metadata=CONNECTOR_ENV_PORT=8081\n ```\n2. Edit the VM from UI and add the startup script mentioned in the automation section.\n You can also use the following gcloud command:\n\n ```bash\n gcloud compute instances add-metadata VM_NAME \\ \n --zone=VM_ZONE \\\n --project=PROJECT_NAME \\\n --metadata-from-file startup-script=gcp-start.sh\n ```\n3. After adding the startup script, ssh to the VM and run the following command: \n\n ```bash\n sudo google_metadata_script_runner startup\n ```\n4. Run the following command to ensure that the docker container is running on the mentioned port. \n\n ```bash\n sudo docker ps\n ```\n5. Run the following command to stop and remove the docker service. After testing, the service must not be running on the VM. \n\n ```bash\n sudo docker rm -f connector-service\n ```\n\nWhat's next\n-----------\n\n - Learn how to [create a VM deployment script](/integration-connectors/docs/marketplace/create-vm-deploy-tf)."]]