Module: Idlc::Deploy::Power

Extended by:
Helpers
Defined in:
lib/idlc-sdk-deploy/power.rb

Defined Under Namespace

Classes: ConnectionError, InstanceKeepAlive

Class Method Summary collapse

Class Method Details

.disable_keep_alive(instance) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/idlc-sdk-deploy/power.rb', line 46

def disable_keep_alive(instance)
  instance.create_tags(
    dry_run: false,
    tags: [ # required
      {
        key: 'keep_alive',
        value: 'false'
      }
    ]
  )
end

.enable_keep_alive(instance) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/idlc-sdk-deploy/power.rb', line 34

def enable_keep_alive(instance)
  instance.create_tags(
    dry_run: false,
    tags: [ # required
      {
        key: 'keep_alive',
        value: "true-#{Time.now.to_i}"
      }
    ]
  )
end

.get_name(tags) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/idlc-sdk-deploy/power.rb', line 119

def get_name(tags)
  name = ''

  tags.each do |t|
    name = t.value if t.key == 'Name'
  end

  # Return
  name
end

.start_instance(instance, async = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/idlc-sdk-deploy/power.rb', line 10

def start_instance(instance, async=false)
  msg("Starting Instance (#{instance.id})...")
  instance.start(
    dry_run: false
  )
  unless async
    obj = instance.wait_until_running
    msg('Started Instance: ' + get_name(obj.tags))
  end
end

.stop_instance(instance, async = false) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/idlc-sdk-deploy/power.rb', line 21

def stop_instance(instance, async=false)
  raise InstanceKeepAlive if keep_alive?(instance.tags)

  msg("Stopping Instance (#{instance.id})...")
  instance.stop(
    dry_run: false
  )
  unless async
    obj = instance.wait_until_stopped
    msg('Stopped Instance: ' + get_name(obj.tags))
  end
end

.update_instance_type(instance, type) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/idlc-sdk-deploy/power.rb', line 58

def update_instance_type(instance, type)
  unless instance.instance_type == type
    name = get_name(instance.tags)
    msg "Changing #{name}: #{instance.instance_type} => #{type}"

    instance.modify_attribute(
      dry_run: false,
      attribute: 'instanceType',
      value: type
    )
  end
end

.wait_for_response(endpoint, success_text = nil, wait_timeout = 1500, sleep_time = 10) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/idlc-sdk-deploy/power.rb', line 96

def wait_for_response(endpoint, success_text = nil, wait_timeout = 1500, sleep_time = 10)
  connected = false
  start_time = Time.now

  until connected
    begin
      if success_text.nil? || success_text == ''
        response = http_request("#{endpoint}/diagnostics/ping")
        connected = success(response.body)
      else
        response = http_request(endpoint)
        connected = simple_success(response.body, success_text)
      end
    rescue ConnectionError, Net::OpenTimeout, JSON::ParserError, Errno::ECONNREFUSED
      check_timeout(start_time, wait_timeout)
      sleep sleep_time
    end
  end

  msg "recieved response from #{endpoint} !"
  true
end

.wait_for_tcp_connection(host, port, connection_timeout = 5, wait_timeout = 1500, sleep_time = 10) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/idlc-sdk-deploy/power.rb', line 71

def wait_for_tcp_connection(host, port, connection_timeout = 5, wait_timeout = 1500, sleep_time = 10)
  connected = false
  start_time = Time.now

  until connected
    begin
      Net::Telnet.new(
        'Host' => host,
        'Port' => port,
        'Telnetmode' => false,
        'Timeout' => connection_timeout
      )

      connected = true
    rescue ConnectionError, Net::OpenTimeout, Errno::ECONNREFUSED
      check_timeout(start_time, wait_timeout)
      debug("waiting for #{host}:#{port} ... (#{(Time.now - start_time)}s elapsed)")
      sleep sleep_time
    end
  end

  msg "recieved response from #{host}:#{port} !"
  true
end