Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use ansible power actions #453

Closed
wants to merge 13 commits into from
12 changes: 12 additions & 0 deletions app/models/manageiq/providers/amazon/cloud_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ class ManageIQ::Providers::Amazon::CloudManager < ManageIQ::Providers::CloudMana
supports :provisioning
supports :regions

def ansible_env_vars
{
'EC2_ACCESS_KEY' => default_authentication.userid,
'EC2_SECRET_KEY' => default_authentication.password,
'EC2_REGION' => provider_region,
}
end

def ansible_root
ManageIQ::Providers::Amazon::Engine.root.join("content_tmp/ansible")
end

def ensure_managers
build_network_manager unless network_manager
network_manager.name = "#{name} Network Manager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ def validate_pause
end

def raw_start
with_provider_object(&:start)
Ansible::Runner.run(ext_management_system.ansible_env_vars,
{:instance_ids => ems_ref},
ext_management_system.ansible_root.join("start_vm.yml"))

# Temporarily update state for quick UI response until refresh comes along
self.update_attributes!(:raw_power_state => "powering_up")
end

def raw_stop
with_provider_object(&:stop)
Ansible::Runner.run(ext_management_system.ansible_env_vars,
{:instance_ids => ems_ref},
ext_management_system.ansible_root.join("stop_vm.yml"))

# Temporarily update state for quick UI response until refresh comes along
self.update_attributes!(:raw_power_state => "shutting_down")
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/manageiq/providers/amazon/network_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ManageIQ::Providers::Amazon::NetworkManager < ManageIQ::Providers::Network

supports_not :ems_network_new

supports :create_security_group

def self.ems_type
@ems_type ||= "ec2_network".freeze
end
Expand All @@ -59,4 +61,8 @@ def self.default_blacklisted_event_names
def self.display_name(number = 1)
n_('Network Provider (Amazon)', 'Network Providers (Amazon)', number)
end

def create_security_group(options)
SecurityGroup.raw_create_security_group(self, options)
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
class ManageIQ::Providers::Amazon::NetworkManager::SecurityGroup < ::SecurityGroup
supports :create

def self.display_name(number = 1)
n_('Security Group (Amazon)', 'Security Groups (Amazon)', number)
end

def self.raw_create_security_group(ext_management_system, options)
Ansible::Runner.run(ext_management_system.parent_manager.ansible_env_vars,
{
:vpc_id => options["vpc_id"],
:security_group_name => options["security_group_name"],
:security_group_description => options["security_group_description"],
:security_group_rules => options["security_group_rules"],
:security_group_rules_egress => options["security_group_rules_egress"],
},
ext_management_system.parent_manager.ansible_root.join("create_security_group.yml"))
rescue => e
_log.error("security_group=[#{options[:name]}], error: #{e}")
raise MiqException::MiqSecurityGroupCreateError, e.message, e.backtrace
end
end
7 changes: 7 additions & 0 deletions content_tmp/ansible/create_security_group.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Create a security group
hosts: localhost
connection: local
gather_facts: False
roles:
- create_security_group
45 changes: 45 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Changing EC2 VM state
=========

Role for changing EC2 VM state

Requirements
------------

Uses https://docs.ansible.com/ansible/latest/modules/ec2_module.html

- python >= 2.6
- boto

Role Variables
--------------

# Required VM state, allowed: [absent, running, restarted, stopped]
vm_state:
# List of instance ids or one id
instance_ids:

Dependencies
------------

None

Example Playbook
----------------

- name: Start VM
hosts: localhost
connection: local
gather_facts: False
vars:
vm_state: running
instance_ids:
- id1
- id2
roles:
- change_vm_state

License
-------

Apache
2 changes: 2 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for change_vm_state
2 changes: 2 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# handlers file for change_vm_state
11 changes: 11 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
galaxy_info:
author: Ladislav Smola
description: Role for changing EC2 VM state
company: Red Hat
license: Apache
min_ansible_version: 2.2
galaxy_tags:
- cloud
- aws
- ec2
dependencies: []
7 changes: 7 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Provision EC2 Instance
local_action:
module: ec2
instance_ids: '{{ instance_ids }}'
state: '{{ vm_state }}'

2 changes: 2 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost

7 changes: 7 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Change VM state
hosts: localhost
connection: local
gather_facts: False
roles:
- change_vm_state
2 changes: 2 additions & 0 deletions content_tmp/ansible/roles/change_vm_state/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# vars file for change_vm_state
38 changes: 38 additions & 0 deletions content_tmp/ansible/roles/create_security_group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Role Name
=========

A brief description of the role goes here.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
roles:
- { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for create_security_group
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# handlers file for create_security_group
57 changes: 57 additions & 0 deletions content_tmp/ansible/roles/create_security_group/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
galaxy_info:
author: your name
description: your description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Some suggested licenses:
# - BSD (default)
# - MIT
# - GPLv2
# - GPLv3
# - Apache
# - CC-BY
license: license (GPLv2, CC-BY, etc)

min_ansible_version: 1.2

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

# Optionally specify the branch Galaxy will use when accessing the GitHub
# repo for this role. During role install, if no tags are available,
# Galaxy will use this branch. During import Galaxy will access files on
# this branch. If Travis integration is configured, only notifications for this
# branch will be accepted. Otherwise, in all cases, the repo's default branch
# (usually master) will be used.
#github_branch:

#
# platforms is a list of platforms, and each platform has a name and a list of versions.
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- name: Create a security group
local_action:
module: ec2_group
vpc_id: "{{ vpc_id }}"
name: "{{ security_group_name }}"
description: "{{ security_group_description }}"
rules: "{{ security_group_rules }}"
rules_egress: "{{ security_group_rules_egress }}"
register: created_security_group
- set_stats:
data:
created_security_group: "{{ created_security_group }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- include: create_security_group.yml
when: security_group_id is not defined or security_group_id == ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost

29 changes: 29 additions & 0 deletions content_tmp/ansible/roles/create_security_group/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- name: Create a security group
hosts: localhost
connection: local
gather_facts: False
roles:
- create_security_group
vars:
msg: |
Module Variables ("vars"):
--------------------------------
{{ vars | to_nice_json }}

Environment Variables ("environment"):
--------------------------------
{{ environment | to_nice_json }}

GROUP NAMES Variables ("group_names"):
--------------------------------
{{ group_names | to_nice_json }}

GROUPS Variables ("groups"):
--------------------------------
{{ groups | to_nice_json }}

HOST Variables ("hostvars"):
--------------------------------
{{ hostvars | to_nice_json }}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# vars file for create_security_group
9 changes: 9 additions & 0 deletions content_tmp/ansible/start_vm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Start VM
hosts: localhost
connection: local
gather_facts: False
vars:
vm_state: running
roles:
- change_vm_state
9 changes: 9 additions & 0 deletions content_tmp/ansible/stop_vm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Stop VM
hosts: localhost
connection: local
gather_facts: False
vars:
vm_state: stopped
roles:
- change_vm_state