Skip to content

Commit a434cce

Browse files
authored
Update cluster state management (#227)
* Fix function return types and dictionary key checks * Update pre-commit Github actions * Update cluster state management, i.e. present, absent, started, restarted, and stopped Signed-off-by: Webster Mudge <[email protected]>
1 parent 232995e commit a434cce

File tree

6 files changed

+399
-193
lines changed

6 files changed

+399
-193
lines changed

.github/workflows/pre-commit.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
name: pre-commit
15+
name: Execute Precommit Linting and Checks
1616

1717
on:
1818
pull_request:
@@ -23,6 +23,6 @@ jobs:
2323
pre-commit:
2424
runs-on: ubuntu-latest
2525
steps:
26-
- uses: actions/checkout@v3
27-
- uses: actions/setup-python@v3
26+
- uses: actions/checkout@v4
27+
- uses: actions/setup-python@v5
2828
- uses: pre-commit/[email protected]

plugins/modules/cluster.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
}
6262

6363
DOCUMENTATION = r"""
64-
---
6564
module: cluster
6665
short_description: Manage the lifecycle and state of a cluster
6766
description:
@@ -77,7 +76,6 @@
7776
"""
7877

7978
EXAMPLES = r"""
80-
---
8179
- name: Create an ECS cluster
8280
cloudera.cluster.cluster:
8381
host: example.cloudera.com
@@ -119,7 +117,6 @@
119117
"""
120118

121119
RETURN = r"""
122-
---
123120
cloudera_manager:
124121
description: Details about Cloudera Manager Cluster
125122
type: dict
@@ -276,21 +273,18 @@ def process(self):
276273

277274
elif self.state == "absent":
278275
# Delete cluster
279-
280276
refresh = False
281-
282-
# TODO Check for status when deleting
283-
# if existing and existing.entity_status == "":
284-
# self.wait_for_active_cmd(cluster_api, self.cluster_name)
285-
# elif existing:
286277
if existing:
287278
self.changed = True
288279
if not self.module.check_mode:
289-
self.cluster_api.delete_cluster(cluster_name=self.name)
290-
self.wait_for_active_cmd(self.name)
280+
if existing.entity_status != "STOPPED":
281+
stop = self.cluster_api.stop_command(cluster_name=self.name)
282+
self.wait_command(stop, polling=self.timeout, delay=self.delay)
283+
284+
delete = self.cluster_api.delete_cluster(cluster_name=self.name)
285+
self.wait_command(delete, polling=self.timeout, delay=self.delay)
291286

292287
elif self.state == "started":
293-
# TODO NONE seems to be fresh cluster, never run before
294288
# Already started
295289
if existing and existing.entity_status == "GOOD_HEALTH":
296290
refresh = False
@@ -312,11 +306,11 @@ def process(self):
312306
# If newly created or created by not yet initialize
313307
if not existing or existing.entity_status == "NONE":
314308
first_run = self.cluster_api.first_run(cluster_name=self.name)
315-
self.wait_for_composite_cmd(first_run.id)
309+
self.wait_command(first_run)
316310
# Start the existing and previously initialized cluster
317311
else:
318312
start = self.cluster_api.start_command(cluster_name=self.name)
319-
self.wait_for_composite_cmd(start.id)
313+
self.wait_command(start, polling=self.timeout, delay=self.delay)
320314

321315
if self.state == "stopped":
322316
# Already stopped
@@ -339,7 +333,7 @@ def process(self):
339333
self.changed = True
340334
if not self.module.check_mode:
341335
stop = self.cluster_api.stop_command(cluster_name=self.name)
342-
self.wait_for_composite_cmd(stop.id)
336+
self.wait_command(stop, polling=self.timeout, delay=self.delay)
343337

344338
if self.state == "restarted":
345339
# Start underway
@@ -357,7 +351,7 @@ def process(self):
357351
self.changed = True
358352
if not self.module.check_mode:
359353
restart = self.cluster_api.restart_command(cluster_name=self.name)
360-
self.wait_for_composite_cmd(restart.id)
354+
self.wait_command(restart, polling=self.timeout, delay=self.delay)
361355

362356
if refresh:
363357
# Retrieve the updated cluster details
@@ -547,7 +541,6 @@ def create_cluster_from_parameters(self):
547541
timeout=self.timeout,
548542
)
549543
parcel.activate()
550-
551544
# Apply host templates
552545
for ht, refs in template_map.items():
553546
self.host_template_api.apply_host_template(
@@ -674,10 +667,10 @@ def create_cluster_from_parameters(self):
674667
if self.auto_assign:
675668
self.cluster_api.auto_assign_roles(cluster_name=self.name)
676669

677-
def marshal_service(self, options: str) -> ApiService:
670+
def marshal_service(self, options: dict) -> ApiService:
678671
service = ApiService(name=options["name"], type=options["type"])
679672

680-
if "display_name" in options:
673+
if options["display_name"]:
681674
service.display_name = options["display_name"]
682675

683676
# Service-wide configuration
@@ -741,9 +734,7 @@ def marshal_hostrefs(self, hosts: dict) -> list[ApiHostRef]:
741734
)
742735
return results
743736

744-
def find_base_role_group_name(
745-
self, service_type: str, role_type: str
746-
) -> ApiRoleConfigGroup:
737+
def find_base_role_group_name(self, service_type: str, role_type: str) -> str:
747738
rcgs = [
748739
rcg
749740
for s in self.service_api.read_services(cluster_name=self.name).items

tests/unit/plugins/modules/cluster/test_cluster.py renamed to tests/unit/plugins/modules/cluster/test_base_cluster.py

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -365,97 +365,6 @@ def test_present_base_host_role_overrides(conn, module_args):
365365
LOG.info(str(e.value.cloudera_manager))
366366

367367

368-
def test_present_basic_cluster(conn, module_args):
369-
args = """
370-
name: Basic_Cluster
371-
cluster_version: "7.1.9-1.cdh7.1.9.p0.44702451"
372-
type: BASE_CLUSTER
373-
state: present
374-
services:
375-
- name: core-settings-0
376-
type: CORE_SETTINGS
377-
display_name: CORE_SETTINGS_TEST
378-
- name: zookeeper-0
379-
type: ZOOKEEPER
380-
display_name: ZK_TEST
381-
config:
382-
zookeeper_datadir_autocreate: yes
383-
- name: hdfs-0
384-
type: HDFS
385-
display_name: HDFS_TEST
386-
config:
387-
zookeeper_service: zookeeper-0
388-
core_connector: core-settings-0
389-
role_groups:
390-
- type: DATANODE
391-
config:
392-
dfs_data_dir_list: /dfs/dn
393-
- type: NAMENODE
394-
config:
395-
dfs_name_dir_list: /dfs/nn
396-
- type: SECONDARYNAMENODE
397-
config:
398-
fs_checkpoint_dir_list: /dfs/snn
399-
- name: yarn-0
400-
type: YARN
401-
display_name: YARN_TEST
402-
config:
403-
hdfs_service: hdfs-0
404-
zookeeper_service: zookeeper-0
405-
role_groups:
406-
- type: RESOURCEMANAGER
407-
config:
408-
yarn_scheduler_maximum_allocation_mb: 4096
409-
yarn_scheduler_maximum_allocation_vcores: 4
410-
- type: NODEMANAGER
411-
config:
412-
yarn_nodemanager_resource_memory_mb: 4096
413-
yarn_nodemanager_resource_cpu_vcores: 4
414-
yarn_nodemanager_local_dirs: /tmp/nm
415-
yarn_nodemanager_log_dirs: /var/log/nm
416-
- type: GATEWAY
417-
config:
418-
mapred_submit_replication: 3
419-
mapred_reduce_tasks: 6
420-
host_templates:
421-
- name: Master1
422-
role_groups:
423-
- service: HDFS
424-
type: NAMENODE
425-
- service: HDFS
426-
type: SECONDARYNAMENODE
427-
- service: YARN
428-
type: RESOURCEMANAGER
429-
- service: YARN
430-
type: JOBHISTORY
431-
- name: Worker
432-
role_groups:
433-
- service: HDFS
434-
type: DATANODE
435-
- service: YARN
436-
type: NODEMANAGER
437-
- service: ZOOKEEPER
438-
type: SERVER
439-
parcels:
440-
CDH: "7.1.9-1.cdh7.1.9.p0.44702451"
441-
hosts:
442-
- name: test10-worker-free-01.cldr.internal
443-
host_template: Master1
444-
- name: test10-worker-free-02.cldr.internal
445-
host_template: Worker
446-
- name: test10-worker-free-03.cldr.internal
447-
host_template: Worker
448-
"""
449-
conn.update(yaml.safe_load(args))
450-
module_args(conn)
451-
452-
with pytest.raises(AnsibleExitJson) as e:
453-
cluster.main()
454-
455-
LOG.info(str(e.value.cloudera_manager))
456-
457-
458-
@pytest.mark.skip(reason="Not yet implemented")
459368
def test_started_base(conn, module_args):
460369
conn.update(
461370
name="PVC-Base",
@@ -470,7 +379,6 @@ def test_started_base(conn, module_args):
470379
LOG.info(str(e.value.cloudera_manager))
471380

472381

473-
@pytest.mark.skip(reason="Not yet implemented")
474382
def test_restarted_base(conn, module_args):
475383
conn.update(
476384
name="PVC-Base",
@@ -485,24 +393,20 @@ def test_restarted_base(conn, module_args):
485393
LOG.info(str(e.value.cloudera_manager))
486394

487395

488-
@pytest.mark.skip(reason="Not yet implemented")
489396
def test_stopped_base(conn, module_args):
490397
conn.update(
491398
name="PVC-Base",
492399
cluster_version="7.1.9", # "1.5.1-b626.p0.42068229",
493-
# type="COMPUTE_CLUSTER",
494400
state="stopped",
495401
)
496402
module_args(conn)
497403

498404
with pytest.raises(AnsibleExitJson) as e:
499405
cluster.main()
500406

501-
# LOG.info(str(e.value))
502407
LOG.info(str(e.value.cloudera_manager))
503408

504409

505-
@pytest.mark.skip(reason="Not yet implemented")
506410
def test_absent_base(conn, module_args):
507411
conn.update(
508412
name="Example_Base",
@@ -516,78 +420,6 @@ def test_absent_base(conn, module_args):
516420
LOG.info(str(e.value.cloudera_manager))
517421

518422

519-
def test_present_compute_minimum(conn, module_args):
520-
conn.update(
521-
name="Example_Compute",
522-
cluster_version="7.1.9",
523-
contexts=["SDX"],
524-
state="present",
525-
)
526-
module_args(conn)
527-
528-
with pytest.raises(AnsibleExitJson) as e:
529-
cluster.main()
530-
531-
LOG.info(str(e.value.cloudera_manager))
532-
533-
534-
@pytest.mark.skip(reason="Not yet implemented")
535-
def test_started_compute_minimum(conn, module_args):
536-
conn.update(
537-
name="Example_Compute",
538-
cluster_version="7.1.9",
539-
contexts=["SDX"],
540-
state="started",
541-
)
542-
module_args(conn)
543-
544-
with pytest.raises(AnsibleExitJson) as e:
545-
cluster.main()
546-
547-
LOG.info(str(e.value.cloudera_manager))
548-
549-
550-
def test_absent_compute(conn, module_args):
551-
conn.update(
552-
name="Example_Compute",
553-
state="absent",
554-
)
555-
module_args(conn)
556-
557-
with pytest.raises(AnsibleExitJson) as e:
558-
cluster.main()
559-
560-
LOG.info(str(e.value.cloudera_manager))
561-
562-
563-
def test_present_experience_minimum(conn, module_args):
564-
conn.update(
565-
name="Example_Experience",
566-
cluster_version="1.5.3",
567-
type="EXPERIENCE_CLUSTER",
568-
state="present",
569-
)
570-
module_args(conn)
571-
572-
with pytest.raises(AnsibleExitJson) as e:
573-
cluster.main()
574-
575-
LOG.info(str(e.value.cloudera_manager))
576-
577-
578-
def test_absent_experience(conn, module_args):
579-
conn.update(
580-
name="Example_Experience",
581-
state="absent",
582-
)
583-
module_args(conn)
584-
585-
with pytest.raises(AnsibleExitJson) as e:
586-
cluster.main()
587-
588-
LOG.info(str(e.value.cloudera_manager))
589-
590-
591423
def test_pytest_cluster_with_template(module_args):
592424
module_args(
593425
{

0 commit comments

Comments
 (0)