Class: CfnManage::StartStopHandler::Transfer

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn_manage/handlers/transfer.rb

Instance Method Summary collapse

Constructor Details

#initialize(server_id, options = {}) ⇒ Transfer

Returns a new instance of Transfer.



8
9
10
11
12
13
14
15
16
# File 'lib/cfn_manage/handlers/transfer.rb', line 8

def initialize(server_id, options = {})
  sftpId = server_id.split("/")
  @server_id = sftpId.last
  credentials = CfnManage::AWSCredentials.get_session_credentials("startstoptransfer_#{@server_id}")
  @client = Aws::Transfer::Client.new(retry_limit: 20)
  if credentials != nil
    @client = Aws::Transfer::Client.new(credentials: credentials, retry_limit: 20)
  end
end

Instance Method Details

#get_stateObject



58
59
60
61
62
63
# File 'lib/cfn_manage/handlers/transfer.rb', line 58

def get_state()
  resp = @client.describe_server({
    server_id: @server_id,
  })
  return resp.server.state
end

#start(configuration) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cfn_manage/handlers/transfer.rb', line 18

def start(configuration)

  state = get_state()

  if state != "OFFLINE"
    $log.warn("SFTP Server #{@server_id} is in a state of #{state} and can not be started.")
    return
  end

  $log.info("Starting SFTP Server #{@server_id}")
  @client.start_server({
    server_id: @server_id,
  })
  unless CfnManage.skip_wait?
    $log.info("Waiting for SFTP to start #{@cluster_id}")
    wait('ONLINE')
  end

  return configuration
end

#stopObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cfn_manage/handlers/transfer.rb', line 39

def stop()

  state = get_state()

  if state != "ONLINE"
    $log.warn("SFTP Server #{@server_id} is in a state of #{state} and can not be stopped.")
    return {}
  end

  @client.stop_server({
    server_id: @server_id,
  })
  unless CfnManage.skip_wait?
    $log.info("Waiting for SFTP to stop #{@cluster_id}")
    wait('OFFLINE')
  end
  return {}
end

#wait(completed_state) ⇒ Object



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
# File 'lib/cfn_manage/handlers/transfer.rb', line 65

def wait(completed_state)

  state_count = 0
  steady_count = 2
  attempts = 0
  until attempts == (max_attempts = 60*6) do
    state = get_state()
    $log.info("SFTP Server #{@server_id} state: #{state}, waiting for #{completed_state}")

    if state == "#{completed_state}"
      state_count = state_count + 1
      $log.info("#{state_count}/#{steady_count}")
    elsif ["START_FAILED", "STOP_FAILED"].include?(state)
      $log.error("SFTP Server #{@server_id} failed to reach state #{completed_state} with current state #{state}!")
      break
    else
      state_count = 0
    end
    break if state_count == steady_count
    attempts = attempts + 1
    sleep(15)
  end

  if attempts == max_attempts
    $log.error("SFTP Server #{@server_id} did not enter #{completed_state} state, however continuing operations...")
  end

end