Class: VpsbClient::Manager

Inherits:
Object
  • Object
show all
Includes:
Client::UploadMetrics
Defined in:
lib/vpsb_client/manager.rb

Defined Under Namespace

Classes: LastMetricNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Client::UploadMetrics

#upload_metrics

Constructor Details

#initialize(config_path, logger = Logger.new(STDOUT)) ⇒ Manager

Returns a new instance of Manager.



13
14
15
16
# File 'lib/vpsb_client/manager.rb', line 13

def initialize(config_path, logger=Logger.new(STDOUT))
  @config_path = config_path
  @logger = logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/vpsb_client/manager.rb', line 9

def config
  @config
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



9
10
11
# File 'lib/vpsb_client/manager.rb', line 9

def http_client
  @http_client
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/vpsb_client/manager.rb', line 9

def logger
  @logger
end

Instance Method Details

#application_idObject

Raises:

  • (NameError)


92
93
94
95
96
97
98
99
# File 'lib/vpsb_client/manager.rb', line 92

def application_id
  return @application_id if @application_id
  id_request = Api::GetItemIdRequest.new(@http_client, 'applications', @config['application_name'])
  http_response = Api::Response.new(id_request.run)
  id = Api::GetItemIdRequest.item_id(http_response)
  raise NameError, "#{@config['application_name']} application not found" unless id
  @application_id = id
end

#close_trial(trial) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vpsb_client/manager.rb', line 112

def close_trial(trial)
  unless enabled?
    logger.debug "[vpsb] not running because vpsb_client is disabled"
    return
  end

  prepare_logfiles

  metric_ids = []
  interval_length = 604800
  last_started_at = trial_last_metric_started_at(trial['id'], interval_length)
  if last_started_at
    start_time = last_started_at + interval_length
  else
    logger.debug "[vpsb] close_trial - no last metric found"
    start_time = Time.now - interval_length
  end
  logger.debug "[vpsb] close_trial - length=#{interval_length} start_time=#{start_time} force=false"
  interval_config = Metrics::IntervalConfig.new(start_time, interval_length, force: true)
  metrics_manager = metrics_manager(trial['id'], interval_config)
  metrics_manager.run
  metric_ids += metrics_manager.created_metric_ids
  logger.debug "[vpsb] Created metric ids: #{metric_ids.inspect}"

  close_request = Api::CloseTrialRequest.new(@http_client, trial['id'])
  http_response = Api::Response.new(close_request.run)
  logger.debug "[vpsb] close request response code = #{http_response.code}"
  http_response
end

#create_sysbench_run(trial_id, test_id, command, data) ⇒ Object



55
56
57
58
59
# File 'lib/vpsb_client/manager.rb', line 55

def create_sysbench_run(trial_id, test_id, command, data)
  create_sysbench_run_request = Api::PostSysbenchRun.new(@http_client, trial_id, test_id, command, data)
  curl_response = create_sysbench_run_request.run
  http_response = Api::Response.new(curl_response)
end

#create_trialObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vpsb_client/manager.rb', line 42

def create_trial
  unless enabled?
    VpsbClient.logger.debug "not running because vpsb_client is disabled"
    return
  end

  builder = Builders::Trial.new(@config, hoster_id, application_id, plan_id)
  create_trial_request = Api::CreateTrialRequest.new(@http_client, builder.create_params)
  curl_response = create_trial_request.run
  http_response = Api::Response.new(curl_response)
  Api::CreateTrialRequest.trial(http_response)
end

#current_trialObject



61
62
63
64
65
66
67
# File 'lib/vpsb_client/manager.rb', line 61

def current_trial
  builder = Builders::Trial.new(@config)
  current_trial_request = Api::GetCurrentTrialRequest.new(@http_client, builder.lookup_params)
  curl_response = current_trial_request.run
  http_response = Api::Response.new(curl_response)
  Api::GetCurrentTrialRequest.trial(http_response)
end

#enabled?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/vpsb_client/manager.rb', line 25

def enabled?
  @config.fetch(:enabled, false)
end

#hoster_idObject

Raises:

  • (NameError)


83
84
85
86
87
88
89
90
# File 'lib/vpsb_client/manager.rb', line 83

def hoster_id
  return @hoster_id if @hoster_id
  id_request = Api::GetItemIdRequest.new(@http_client, 'hosters', @config['hoster_name'])
  http_response = Api::Response.new(id_request.run)
  id = Api::GetItemIdRequest.item_id(http_response)
  raise NameError, "#{@config['hoster_name']} hoster not found" unless id
  @hoster_id = id
end

#plan_idObject

Raises:

  • (NameError)


101
102
103
104
105
106
107
108
# File 'lib/vpsb_client/manager.rb', line 101

def plan_id
  return @plan_id if @plan_id
  id_request = Api::GetPlanIdRequest.new(@http_client, hoster_id, @config['plan_name'])
  http_response = Api::Response.new(id_request.run)
  id = Api::GetPlanIdRequest.item_id(http_response)
  raise NameError, "#{@config['plan_name']} plan not found" unless id
  @plan_id = id
end

#setupObject



18
19
20
21
22
23
# File 'lib/vpsb_client/manager.rb', line 18

def setup
  VpsbClient.logger = @logger
  @config = Config.new(@config_path)
  @curl_wrapper = CurlWrapper.new(@config['auth_token'])
  @http_client = HttpClient.new(@curl_wrapper, @config['vpsb_protocol'], @config['vpsb_hostname'])
end

#signed_in?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vpsb_client/manager.rb', line 29

def signed_in?
  return @signed_in if @signed_in
  id_request = Api::GetItemIdRequest.new(@http_client, 'hosters', 'linode')
  curl_response = id_request.run

  begin
    http_response = Api::Response.new(curl_response)
  rescue Api::Response::NotAuthenticated => e
    return @signed_in = false
  end
  @signed_in = true
end

#trial_last_metric_started_at(trial_id, length) ⇒ Object



69
70
71
72
73
74
# File 'lib/vpsb_client/manager.rb', line 69

def trial_last_metric_started_at(trial_id, length)
  current_trial_request = Api::GetTrialLastMetricRequest.new(@http_client, { trial_id: trial_id, length: length})
  curl_response = current_trial_request.run
  http_response = Api::Response.new(curl_response)
  Api::GetTrialLastMetricRequest.started_at(http_response)
end

#trial_sysbench_tests(trial_id) ⇒ Object



76
77
78
79
80
81
# File 'lib/vpsb_client/manager.rb', line 76

def trial_sysbench_tests(trial_id)
  trial_sysbench_tests_request = Api::GetTrialSysbenchTests.new(@http_client, trial_id: trial_id)
  curl_response = trial_sysbench_tests_request.run
  http_response = Api::Response.new(curl_response)
  Api::GetTrialSysbenchTests.tests(http_response)
end