-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.rb
188 lines (164 loc) · 6.64 KB
/
client.rb
1
2
3
4
5
6
7
8
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
35
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
99
100
101
102
103
104
105
106
107
108
109
110
111
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require 'net/http'
require 'suse/toolkit/utilities'
module SUSE
module Connect
# Client to interact with API
class Client
include SUSE::Toolkit::Utilities
include Logger
attr_reader :config, :api
# builds an instance of the client
#
# @param client [Config] populated config merged with overwrites on top of those being read from config file
#
# @return [Client]
def initialize(config)
@config = config
@api = Api.new(self)
log.debug "Merged options: #{@config}"
end
# Announces the system, activates the product on SCC and adds the service to the system
def register!
announce_or_update
product = @config.product || Zypper.base_product
service = activate_product(product, @config.email)
System.add_service(service)
Zypper.install_release_package(product.identifier) if @config.product
print_success_message product
end
# Deregisters a whole system or a single product
#
# @returns: Empty body and 204 status code
def deregister!
raise SystemNotRegisteredError unless registered?
if @config.product
service = deactivate_product @config.product
System.remove_service service
Zypper.remove_release_package @config.product.identifier
print_success_message @config.product, action: 'Deregistered'
else
@api.deregister(system_auth)
System.cleanup!
log.info 'Successfully deregistered system.'
end
end
# Announce system via SCC/Registration Proxy
#
# @returns: [Array] login, password tuple. Those credentials are given by SCC/Registration Proxy
def announce_system(distro_target = nil, instance_data_file = nil)
instance_data = System.read_file(instance_data_file) if instance_data_file
params = [token_auth(@config.token), distro_target, instance_data]
params.push(@config.namespace) if @config.namespace
response = @api.announce_system(*params)
[response.body['login'], response.body['password']]
end
# Re-send the system's hardware details on SCC
#
def update_system(distro_target = nil, instance_data_file = nil)
instance_data = System.read_file(instance_data_file) if instance_data_file
params = [system_auth, distro_target, instance_data]
params.push(@config.namespace) if @config.namespace
@api.update_system(*params)
end
# Activate a product
#
# @param product [SUSE::Connect::Zypper::Product]
# @returns: Service for this product
def activate_product(product, email = nil)
result = @api.activate_product(system_auth, product, email).body
Remote::Service.new(result)
end
# Deactivate a product
#
# @param product [SUSE::Connect::Remote::Product]
# @returns: Service for this product
def deactivate_product(product)
result = @api.deactivate_product(system_auth, product).body
Remote::Service.new(result)
end
# Upgrade a product
# System upgrade (eg SLES11 -> SLES12) without regcode
#
# @param product [Remote::Product] desired product to be upgraded
# @returns: Service for this product
def upgrade_product(product)
result = @api.upgrade_product(system_auth, product).body
Remote::Service.new(result)
end
# Downgrade a product
# System downgrade (eg SLES12 SP1 -> SLES12) without regcode
#
# @param product [Remote::Product] desired product to be upgraded
# @returns: Service for this product
alias_method :downgrade_product, :upgrade_product
# Synchronize system products with registration server
#
# @param products [Array] List of activated system products to synchronize
def synchronize(products)
@api.synchronize(system_auth, products).body
end
# @param product [Remote::Product] product to query extensions for
def show_product(product)
result = @api.show_product(system_auth, product).body
Remote::Product.new(result)
end
# @returns: body described in https://github.com/SUSE/connect/wiki/SCC-API-(Implemented)#response-12 and
# 200 status code
def system_services
@api.system_services(system_auth)
end
# @returns: body described in https://github.com/SUSE/connect/wiki/SCC-API-(Implemented)#response-13 and
# 200 status code
def system_subscriptions
@api.system_subscriptions(system_auth)
end
# @returns: body described in https://github.com/SUSE/connect/wiki/SCC-API-(Implemented)#response-14 and
# 200 status code
def system_activations
@api.system_activations(system_auth)
end
# Lists all available upgrade paths for a given list of products
#
# @param [Array <Remote::Product>] the list of currently installed products in the system
#
# @return [Array <Array <Remote::Product>>] the list of possible upgrade paths for the given products,
# where an upgrade path is an array of Remote::Product objects.
def system_migrations(products)
upgrade_paths = @api.system_migrations(system_auth, products).body
upgrade_paths.map do |upgrade_path|
upgrade_path.map do |product_attributes|
Remote::Product.new(product_attributes)
end
end
end
# List available Installer-Updates repositories for the given product
#
# @param product [Remote::Product] list repositories for this product
#
# @return [Array <Hash>] list of Installer-Updates repositories
def list_installer_updates(product)
@api.list_installer_updates(product).body
end
private
# Announces the system to the server, receiving and storing its credentials.
# When already announced, sends the current hardware details to the server
def announce_or_update
if registered?
update_system
else
login, password = announce_system(nil, @config.instance_data_file)
Credentials.new(login, password, Credentials.system_credentials_file).write
end
end
def registered?
System.credentials?
end
def print_success_message(product, action: 'Registered')
log.info "#{action} #{product.identifier} #{product.version} #{product.arch}"
log.info "Rooted at: #{@config.filesystem_root}" if @config.filesystem_root
log.info "To server: #{@config.url}" if @config.url
log.info "Using E-Mail: #{@config.email}" if @config.email
end
end
end
end