Nate Stephany
2019-12-10 18d1d24c8e12c1530fab91d2655c9157842c3b58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
# This file is the default playbook for common actions.
# You should implement those actions in your config if you
# need a specific process.
 
- import_playbook: setup_runtime.yml
 
- name: Run stop/start/status/... actions
  hosts: localhost
  connection: local
  gather_facts: False
  become: no
  tasks:
    - fail:
        msg: "project_tag is not defined"
      when: project_tag is not defined or project_tag == ''
 
    - fail:
        msg: "ACTION is not defined"
      when: ACTION is not defined
 
    - when: >-
        guid is not defined
        or guid == ''
        or guid == '*'
      fail:
        msg: variable 'guid' must be defined and not empty
 
    - when:
        - cloud_provider == 'ec2'
        - guid is defined
        - guid != ''
        - guid != '*'
      environment:
        AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
        AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
        AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
      block:
        - when: ACTION == 'stop'
          name: Stop instances by (guid,env_type) tags
          ec2_instance:
            state: stopped
            wait: no
            filters:
              "tag:guid": "{{ guid }}"
              # TODO: uncomment this after a few weeks
              #"tag:env_type": "{{ env_type }}"
 
        - when: ACTION == 'start'
          name: Start instances by (guid, env_type) tags
          ec2_instance:
            state: started
            wait: no
            filters:
              "tag:guid": "{{ guid }}"
              # TODO: uncomment this after a few weeks
              #"tag:env_type": "{{ env_type }}"
 
        - when: ACTION == 'status'
          block:
            - name: Get EC2 facts using (guid, env_type) tag
              ec2_instance_facts:
                filters:
                  "tag:guid": "{{ guid }}"
                  # TODO: uncomment this after a few weeks
                  #"tag:env_type": "{{ env_type }}"
              register: r_instances
 
            - name: Print status information to a file
              copy:
                dest: "{{ output_dir }}/status.txt"
                content: |-
                  {{ "%-60s" | format('Instance') }} State      Type
                  ----------------------------------------------------------------
                  {% for instance in r_instances.instances %}
                  {{ "%-60s" | format(instance.tags.Name) }} {{ "%-10s" | format(instance.state.name) }} {{ instance.instance_type }}
                  {% endfor %}
    - when: cloud_provider == 'osp'
      environment:
        OS_AUTH_URL: "{{ osp_auth_url }}"
        OS_USERNAME: "{{ osp_auth_username }}"
        OS_PASSWORD: "{{ osp_auth_password }}"
        OS_PROJECT_NAME: "{{ osp_project_name }}"
        OS_PROJECT_DOMAIN_ID: "{{ osp_auth_project_domain }}"
        OS_USER_DOMAIN_NAME: "{{ osp_auth_user_domain }}"
      block:
        - when: ACTION == 'stop'
          block:
            - name: Gather instance facts
              os_server_facts:
                filters:
                  metadata:
                    guid: "{{ guid }}"
                    env_type: "{{ env_type }}"
                  vm_state: active
              register: r_osp_facts
 
            - when: r_osp_facts.ansible_facts.openstack_servers | length > 0
              block:
                - set_fact:
                    all_instances: >-
                      {{ r_osp_facts.ansible_facts.openstack_servers
                      | json_query('[*].id') }}
 
                - name: Stop all servers
                  command: openstack server stop {{ all_instances | join(' ') }}
 
        - when: ACTION == 'start'
          block:
            - name: Gather instance facts
              os_server_facts:
                filters:
                  metadata:
                    guid: "{{ guid }}"
                    env_type: "{{ env_type }}"
                  vm_state: stopped
              register: r_osp_facts
 
            - when: r_osp_facts.ansible_facts.openstack_servers | length > 0
              block:
                - set_fact:
                    all_instances: >-
                      {{ r_osp_facts.ansible_facts.openstack_servers
                      | json_query('[*].id') }}
 
                - name: Start all servers
                  command: openstack server start {{ all_instances | join(' ') }}
 
        - when: ACTION == 'status'
          block:
            - name: Get OSP facts using (guid, env_type) metadata
              os_server_facts:
                filters:
                  metadata:
                    guid: "{{ guid }}"
                    env_type: "{{ env_type }}"
              register: r_instances
 
            - debug:
                var: r_instances
 
            - name: Print status information to a file
              copy:
                dest: "{{ output_dir }}/status.txt"
                content: |-
                  {{ "%-30s" | format('Instance') }} State
                  -------------------------------------------
                  {% for instance in r_instances.ansible_facts.openstack_servers %}
                  {{ "%-30s" | format(instance.name) }} {{ "%-10s" | format(instance.vm_state) }}
                  {% endfor %}