From 9f7398ff773a672caf18e02e92c9e11769091d84 Mon Sep 17 00:00:00 2001 From: Jack Ivanov Date: Sat, 17 Feb 2018 21:36:14 +0300 Subject: [PATCH] Scaleway integration #623 --- algo | 44 +++++++++- config.cfg | 4 + deploy.yml | 3 +- roles/cloud-scaleway/tasks/main.yml | 128 ++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 roles/cloud-scaleway/tasks/main.yml diff --git a/algo b/algo index e7e1dd22..cb9559ac 100755 --- a/algo +++ b/algo @@ -347,6 +347,42 @@ algo_region=${algo_region:-1} EXTRA_VARS="aws_access_key=$aws_access_key aws_secret_key=$aws_secret_key algo_server_name=$algo_server_name region=$region" } +scaleway () { +read -p " +Enter your auth token (https://www.scaleway.com/docs/generate-an-api-token/) +$ADDITIONAL_PROMPT +[...]: " -rs scaleway_auth_token + +read -p " + +Enter your organization name (https://cloud.scaleway.com/#/billing) +$ADDITIONAL_PROMPT +[...]: " -rs scaleway_organization + +read -p " + +Name the vpn server: +[algo.local]: " -r algo_server_name + algo_server_name=${algo_server_name:-algo.local} + + read -p " + + What region should the server be located in? + 1. par1 Paris + 2. ams1 Amsterdam +Enter the number of your desired region: +[1]: " -r algo_region +algo_region=${algo_region:-1} + + case "$algo_region" in + 1) region="par1" ;; + 2) region="ams1" ;; + esac + + ROLES="scaleway vpn cloud" + EXTRA_VARS="scaleway_auth_token=$scaleway_auth_token scaleway_organization=\"$scaleway_organization\" algo_server_name=$algo_server_name algo_region=$region" +} + gce () { read -p " Enter the local path to your credentials JSON file (https://support.google.com/cloud/answer/6158849?hl=en&ref_topic=6262490#serviceaccounts): @@ -494,7 +530,8 @@ algo_provisioning () { 3. Amazon EC2 4. Microsoft Azure 5. Google Compute Engine - 6. Install to existing Ubuntu 16.04 server + 6. Scaleway + 7. Install to existing Ubuntu 16.04 server Enter the number of your desired provider : " @@ -503,11 +540,12 @@ Enter the number of your desired provider case "$N" in 1) digitalocean; ;; - 2) lightsail; ;; + 2) lightsail; ;; 3) ec2; ;; 4) azure; ;; 5) gce; ;; - 6) non_cloud; ;; + 6) scaleway; ;; + 7) non_cloud; ;; *) exit 1 ;; esac diff --git a/config.cfg b/config.cfg index f42cfd75..7c090867 100644 --- a/config.cfg +++ b/config.cfg @@ -89,6 +89,10 @@ cloud_providers: lightsail: size: nano_1_0 image: ubuntu_16_04 + scaleway: + size: VC1S + image: Ubuntu Xenial + arch: x86_64 local: fail_hint: diff --git a/deploy.yml b/deploy.yml index a1cbec4f..dd39c273 100644 --- a/deploy.yml +++ b/deploy.yml @@ -27,6 +27,7 @@ - { role: cloud-gce, tags: ['gce'] } - { role: cloud-azure, tags: ['azure'] } - { role: cloud-lightsail, tags: ['lightsail'] } + - { role: cloud-scaleway, tags: ['scaleway'] } - { role: local, tags: ['local'] } post_tasks: @@ -53,7 +54,7 @@ - block: - name: Common pre-tasks include_tasks: playbooks/common.yml - tags: [ 'digitalocean', 'ec2', 'gce', 'azure', 'lightsail', 'local', 'pre' ] + tags: [ 'digitalocean', 'ec2', 'gce', 'azure', 'lightsail', 'scaleway', 'local', 'pre' ] rescue: - debug: var=fail_hint tags: always diff --git a/roles/cloud-scaleway/tasks/main.yml b/roles/cloud-scaleway/tasks/main.yml new file mode 100644 index 00000000..ca4e4e6d --- /dev/null +++ b/roles/cloud-scaleway/tasks/main.yml @@ -0,0 +1,128 @@ +- block: + - name: Check if server exists + uri: + url: "https://cp-{{ algo_region }}.scaleway.com/servers" + method: GET + headers: + Content-Type: 'application/json' + X-Auth-Token: "{{ scaleway_auth_token }}" + status_code: 200 + register: scaleway_servers + + - name: Set server id as a fact + set_fact: + server_id: "{{ item.id }}" + no_log: true + when: algo_server_name == item.name + with_items: "{{ scaleway_servers.json.servers }}" + + - name: Create a server if it doesn't exist + block: + - name: Get the organization id + uri: + url: https://account.cloud.online.net/organizations + method: GET + headers: + Content-Type: 'application/json' + X-Auth-Token: "{{ scaleway_auth_token }}" + status_code: 200 + register: scaleway_organizations + + - name: Set organization id as a fact + set_fact: + organization_id: "{{ item.id }}" + no_log: true + when: scaleway_organization == item.name + with_items: "{{ scaleway_organizations.json.organizations }}" + + - name: Get images + uri: + url: "https://cp-{{ algo_region }}.scaleway.com/images" + method: GET + headers: + Content-Type: 'application/json' + X-Auth-Token: "{{ scaleway_auth_token }}" + status_code: 200 + register: scaleway_images + + - name: Set image id as a fact + set_fact: + image_id: "{{ item.id }}" + no_log: true + when: + - cloud_providers.scaleway.image in item.name + - cloud_providers.scaleway.arch == item.arch + with_items: "{{ scaleway_images.json.images }}" + + - name: Create a server + uri: + url: "https://cp-{{ algo_region }}.scaleway.com/servers/" + method: POST + headers: + Content-Type: 'application/json' + X-Auth-Token: "{{ scaleway_auth_token }}" + body: + organization: "{{ organization_id }}" + name: "{{ algo_server_name }}" + image: "{{ image_id }}" + commercial_type: "{{cloud_providers.scaleway.size }}" + tags: + - Environment:Algo + - AUTHORIZED_KEY={{ lookup('file', SSH_keys.public)|regex_replace(' ', '_') }} + enable_ipv6: true + status_code: 201 + body_format: json + register: algo_instance + + - name: Set server id as a fact + set_fact: + server_id: "{{ algo_instance.json.server.id }}" + when: server_id is not defined + + - name: Power on the server + uri: + url: https://cp-{{ algo_region }}.scaleway.com/servers/{{ server_id }}/action + method: POST + headers: + Content-Type: application/json + X-Auth-Token: "{{ scaleway_auth_token }}" + body: + action: poweron + status_code: 202 + body_format: json + ignore_errors: true + no_log: true + + - name: Wait for the server to become running + uri: + url: "https://cp-{{ algo_region }}.scaleway.com/servers/{{ server_id }}" + method: GET + headers: + Content-Type: 'application/json' + X-Auth-Token: "{{ scaleway_auth_token }}" + status_code: 200 + until: + - algo_instance.json.server.state is defined + - algo_instance.json.server.state == "running" + retries: 20 + delay: 30 + register: algo_instance + + - set_fact: + cloud_instance_ip: "{{ algo_instance['json']['server']['public_ip']['address'] }}" + + - name: Add new instance to host group + add_host: + hostname: "{{ cloud_instance_ip }}" + groupname: vpn-host + ansible_ssh_user: root + ansible_python_interpreter: "/usr/bin/python2.7" + ansible_ssh_private_key_file: "{{ SSH_keys.private }}" + cloud_provider: scaleway + ipv6_support: yes + + rescue: + - debug: var=fail_hint + tags: always + - fail: + tags: always