Class: ForemanRhCloud::CloudConnector

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_rh_cloud/cloud_connector.rb

Constant Summary collapse

CLOUD_CONNECTOR_USER =
'cloud_connector_user'.freeze
CLOUD_CONNECTOR_TOKEN_NAME =
'Cloud connector access token'.freeze
CLOUD_CONNECTOR_FEATURE =
'ansible_configure_cloud_connector'.freeze

Instance Method Summary collapse

Instance Method Details

#foreman_hostObject



70
71
72
# File 'app/services/foreman_rh_cloud/cloud_connector.rb', line 70

def foreman_host
  ForemanRhCloud.foreman_host
end

#installObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/foreman_rh_cloud/cloud_connector.rb', line 9

def install
  user = service_user
  token_value = personal_access_token(user)
  target_host = foreman_host
  composer = nil

  input = {
    :satellite_cloud_connector_user => service_user.,
    :satellite_cloud_connector_password => token_value,
  }

  if (http_proxy = ForemanRhCloud.proxy_setting(logger: Foreman::Logging.logger('app')))
    input[:satellite_cloud_connector_http_proxy] = http_proxy
  end

  Taxonomy.as_taxonomy(target_host.organization, target_host.location) do
    composer = ::JobInvocationComposer.for_feature(
      CLOUD_CONNECTOR_FEATURE,
      [target_host.id],
      input
    )
    composer.trigger!
  end

  composer.job_invocation
end

#latest_jobObject



36
37
38
39
40
# File 'app/services/foreman_rh_cloud/cloud_connector.rb', line 36

def latest_job
  feature_id = RemoteExecutionFeature.find_by_label(CLOUD_CONNECTOR_FEATURE)&.id
  return nil unless feature_id
  JobInvocation.where(:remote_execution_feature_id => feature_id).includes(:task).reorder('foreman_tasks_tasks.started_at DESC').first
end

#personal_access_token(user) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/foreman_rh_cloud/cloud_connector.rb', line 42

def personal_access_token(user)
  access_token = PersonalAccessToken.find_by_name(CLOUD_CONNECTOR_TOKEN_NAME)

  access_token&.destroy! # destroy the old token if exists

  personal_access_token = PersonalAccessToken.new(:name => CLOUD_CONNECTOR_TOKEN_NAME, :user => user)
  token_value = personal_access_token.generate_token
  personal_access_token.save!

  token_value
end

#service_userObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/foreman_rh_cloud/cloud_connector.rb', line 54

def service_user
  user = User.(CLOUD_CONNECTOR_USER)

  if user.nil?
    user = User.create!(
      :login => CLOUD_CONNECTOR_USER,
      :password => SecureRandom.base64(255),
      :description => "This is a service user used by cloud connector to talk to the Satellite API",
      :auth_source => AuthSourceInternal.first,
      :admin => true
    )
  end

  user
end