Skip to content

Commit

Permalink
Merge pull request #565 from tonytan4ever/CEPHSTORA-453
Browse files Browse the repository at this point in the history
Cephstora 453
  • Loading branch information
tonytan4ever authored Oct 2, 2018
2 parents 6d03412 + e680823 commit 0f2e01a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 7 deletions.
43 changes: 37 additions & 6 deletions playbooks/library/raxmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
options:
cmd:
description: The command to run
choices = [ 'assign_agent_to_entity', 'create_agent_token' ]
choices = [ 'assign_agent_to_entity', 'create_agent_token',
'delete_entity']
required: true
entity:
description: The label of the entity to operate against
Expand All @@ -50,11 +51,17 @@
cmd: assign_agent_to_entity
entity: controller1
venv_bin: /openstack/venvs/maas-r14.1.0rc1/bin/
create_entity_if_not_exists: true
raxmon:
cmd: create_agent_token
entity: controller1
venv_bin: /openstack/venvs/maas-r14.1.0rc1/bin/
raxmon:
cmd: delete_entity
entity: controller1
venv_bin: /openstack/venvs/maas-r14.1.0rc1/bin/
"""

RETURN = """
Expand Down Expand Up @@ -90,16 +97,20 @@ def _get_conn(get_driver, provider_cls, raxmon_cfg):
return conn


def _get_entities(conn, entity):
def _get_entities(conn, entity, create_entity_if_not_exists=False):
entities = []
for e in conn.list_entities():
if e.label == entity:
entities.append(e)
# create entity if needed
if create_entity_if_not_exists and len(entities) == 0:
created = conn.create_entity(label=entity)
entities = [created]
return entities


def assign_agent_to_entity(module, conn, entity):
entities = _get_entities(conn, entity)
def assign_agent_to_entity(module, conn, entity, create_entity_if_not_exists):
entities = _get_entities(conn, entity, create_entity_if_not_exists)
entities_count = len(entities)
if entities_count == 0:
msg = "Zero entities with the label %s exist. Entities should be " \
Expand Down Expand Up @@ -139,15 +150,32 @@ def create_agent_token(module, conn, entity):
module.fail_json(msg=msg)


def delete_entity(module, conn, entity):
entities = _get_entities(conn, entity)
for entity in entities:
try:
conn.delete_entity(entity)
except Exception as e:
msg = "Deleting entity: %s failed. Reason:\n" % entity.label
msg += str(e.message)
module.exit_json(changed=False, msg=msg)
module.exit_json(changed=True)


def main():
module = AnsibleModule(
argument_spec=dict(
cmd=dict(
choices=['assign_agent_to_entity', 'create_agent_token'],
choices=['assign_agent_to_entity', 'create_agent_token',
'delete_entity'],
required=True
),
entity=dict(required=True),
venv_bin=dict(),
create_entity_if_not_exists=dict(
type='bool',
default=False
),
raxmon_cfg=dict(default='/root/.raxrc')
)
)
Expand Down Expand Up @@ -176,9 +204,12 @@ def main():
conn = _get_conn(get_driver, Provider, module.params['raxmon_cfg'])

if module.params['cmd'] == 'assign_agent_to_entity':
assign_agent_to_entity(module, conn, module.params['entity'])
assign_agent_to_entity(module, conn, module.params['entity'],
module.params['create_entity_if_not_exists'])
elif module.params['cmd'] == 'create_agent_token':
create_agent_token(module, conn, module.params['entity'])
elif module.params['cmd'] == 'delete_entity':
delete_entity(module, conn, module.params['entity'])


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions playbooks/maas-agent-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
cmd: create_agent_token
entity: "{{ maas_entity_name }}"
venv_bin: "{{ maas_venv_bin }}"
create_entity_if_not_exists: "{{ create_entity_if_not_exists | default(False) }}"
register: agent_token
when:
- maas_use_api | bool
Expand Down
2 changes: 1 addition & 1 deletion tests/test-ansible-functional.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ echo "TEST_IDEMPOTENCE: ${TEST_IDEMPOTENCE}"

function set_ansible_parameters {
# NOTE(tonytan4ever): We always skip preflight metadata check
ANSIBLE_CLI_PARAMETERS="-e maas_pre_flight_metadata_check_enabled=false"
ANSIBLE_CLI_PARAMETERS="-e maas_pre_flight_metadata_check_enabled=false -e create_entity_if_not_exists=true -e cleanup_entity=true"

if [ "${ANSIBLE_PARAMETERS}" != false ]; then
ANSIBLE_CLI_PARAMETERS+=" ${ANSIBLE_PARAMETERS}"
Expand Down
31 changes: 31 additions & 0 deletions tests/test-raxmon-assign-agent-to-entity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
# Copyright 2018, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Delete Entity ID for test clean up
hosts: localhost
connection: local
user: "{{ ansible_user | default('root') }}"
tasks:
- name: Assign agent to entity.
raxmon:
cmd: assign_agent_to_entity
entity: "{{ maas_entity_name }}"
venv_bin: "{{ maas_venv_bin }}"
create_entity_if_not_exists: "{{ create_entity_if_not_exists | default(False) }}"

vars_files:
- ../playbooks/vars/main.yml


28 changes: 28 additions & 0 deletions tests/test-raxmon-delete-entity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
# Copyright 2018, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Delete Entity for test clean up
hosts: hosts[0]
user: "{{ ansible_user | default('root') }}"
tasks:
- name: Delete entity to clean up
raxmon:
cmd: delete_entity
entity: "{{ maas_entity_name }}"
venv_bin: "{{ maas_venv_bin }}"
# when:
# - cleanup_entity | default(False)
vars_files:
- ../playbooks/vars/main.yml
1 change: 1 addition & 0 deletions tests/test-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
copy:
src: "user_{{ ansible_env.RE_JOB_SCENARIO | default('master') }}_vars.yml"
dest: "/etc/openstack_deploy/user_rpcm_{{ ansible_env.RE_JOB_SCENARIO | default('master') }}_vars.yml"

- name: Copy RPC-M secrets into place
copy:
src: "user_rpcm_secrets.yml"
Expand Down
3 changes: 3 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

# Run the tigk stack playbooks
- include: "../playbooks/maas-tigkstack-all.yml"

# Run the entity deletion playbook
- include: "test-raxmon-delete-entity.yml"
3 changes: 3 additions & 0 deletions tests/test_with_maas_rally.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@

# Run the tigk stack playbooks
- include: "../playbooks/maas-tigkstack-all.yml"

# Run the entity deletion playbook
- include: "test-raxmon-delete-entity.yml"

0 comments on commit 0f2e01a

Please sign in to comment.