Puppet Plan: oci_config::shutdown_instances

Defined in:
plans/shutdown_instances.pp

Summary

Use this plan to correctly shutdown an OCI instance on OS level and then stop the instance on OCI level. This is usefull for making

Overview

See the file “LICENSE” for the full license governing this code.

--

–++–

Examples:

bolt plan run oci_config::shutdowm_instances oci_master=mypupptserver instances=mynode1,mynode2,mynode3

Parameters:

  • oci_master (TargetSpec) (defaults to: 'localhost')

    The Puppetserver that has the oci_config module installed and has one or more oci_tenant instances defined on it.

  • instances (TargetSpec)

    The OCI instance names you want to shutdown

  • shutdown_message (String[1]) (defaults to: 'Shutting down for cloud cost control')

    The shutdown message to display on the specified node

  • after (Integer) (defaults to: 10)

    The time in seconds to wait before actually start the shutdown.

  • wait_time (Integer) (defaults to: 180)

    The time in seconds to wait for the system to be shutdown. After this wait time is exceded and the system is still not down, continue processing.

  • retry_interval (Integer) (defaults to: 10)

    The retry interval between checks if the system is really down.



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
# File 'plans/shutdown_instances.pp', line 36

plan oci_config::shutdown_instances(
  TargetSpec $instances,
  TargetSpec $oci_master        = 'localhost',
  String[1]  $shutdown_message  = 'Shutting down for cloud cost control',
  Integer    $after             = 10,
  Integer    $wait_time         = 180,
  Integer    $retry_interval    = 10,
) {
  if get_targets($oci_master).length > 1 {
    fail_plan("${oci_master} did not resolve to a single target")
  }

  $instance_nodes = get_targets($instances)

  $instance_nodes.each |$instance| {
    $hostname = $instance.host
    $node_facts = puppetdb_fact([$hostname])
    if $node_facts == {} {
      warning("Skipping node ${instance.host} not found in Puppetdb.")
    } elsif $node_facts.dig($hostname, 'oci_instance_id') == undef {
      warning("Skipping node ${instance.host} not an OCI node.")
    } else {
      $instance_id = $node_facts.dig($hostname, 'oci_instance_id')
      notice("Execute regular shutdown on machine ${hostname}...")

      #
      # Initiate the OS schutdown on the system
      #
      without_default_logging() || {
        #
        # The reboot tasks gives people the impression that it is a reboot
        # So that's why we're running without_default_logging
        #
        run_task('reboot', $instance,
          message       => $shutdown_message,
          timeout       => $after,
          shutdown_only => true,
          _catch_errors  => true)
      }
      #
      # We might want to wait until the OS shtdown is complete
      # before swithcing off the machine
      #
      if $wait_time > 0 {
        notice("Waiting for shutdown to complete on ${hostname}...")
        oci_config::wait_until_unavailable($hostname,
          wait_time      => $wait_time,
          retry_interval => $retry_interval,
          _catch_errors  => true)
      } else {
        notice("Skipped waiting for shutdown to complete on ${hostname}...")
      }
      #
      # Now do the OCI shutdown
      #
      notice("Execute shutdown on OCI-level for ${hostname} with ocid ${instance_id}...")
      run_task('oci_config::instance_action',
        $oci_master,
        instance_id => $instance_id,
        action => 'SOFTSTOP' )
    }
  }
}