Class: DTK::Network::Client::Command::Install

Inherits:
DTK::Network::Client::Command show all
Includes:
Util::OsUtil, Util::Tar
Defined in:
lib/client/command/install.rb,
lib/client/command/install/installer.rb

Defined Under Namespace

Modules: Installer

Constant Summary

Constants included from Util::OsUtil

Util::OsUtil::DTK_HOME_DIR, Util::OsUtil::DTK_MODULES_DIR, Util::OsUtil::DTK_MODULES_GZIP_DIR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::OsUtil

#current_dir, #delim, #dtk_local_folder, #dtk_modules_gzip_location, #dtk_modules_location, #home_dir, #temp_dir

Methods included from Util::Tar

#gzip, #tar, #ungzip, #untar

Methods inherited from DTK::Network::Client::Command

wrap_command

Methods included from RestWrapper

#rest_delete, #rest_get, #rest_post

Methods included from Client::PermissionsUtil

#validate_permissions!

Constructor Details

#initialize(module_ref, dependency_tree, options = {}) ⇒ Install

Returns a new instance of Install.



7
8
9
10
11
12
13
14
15
16
# File 'lib/client/command/install.rb', line 7

def initialize(module_ref, dependency_tree, options = {})
  @module_ref       = module_ref
  @dependency_tree  = dependency_tree
  @module_directory = module_ref.repo_dir
  @explicit_path    = module_ref.explicit_path
  @options          = options
  @parsed_module    = options[:parsed_module]
  @ret              = []
  @type             = options[:type]
end

Class Method Details

.run(module_info, opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/client/command/install.rb', line 18

def self.run(module_info, opts = {})
  module_ref      = ModuleRef.new(module_info)

  begin
    dependency_tree = DependencyTree.get_or_create(module_ref, opts.merge(format: :hash))
  rescue Error::DependencyError => ex
    raise Error::DependencyError, "#{ex.message}Use flag --download-if-fail to download module content to your local environment to fix the problem." unless opts[:download_if_fail]

    new(module_ref, [], opts).install
    raise ex
  end

  new(module_ref, dependency_tree, opts).install
end

Instance Method Details

#construct_clone_url(module_info) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/client/command/install.rb', line 169

def construct_clone_url(module_info)
  require 'open-uri'

  public_user_meta = module_info['public_user_meta']
  codecommit_uri   = module_info['codecommit_uri']

  if codecommit_uri
    codecommit_data   = public_user_meta || Session.get_codecommit_data
    service_user_name = codecommit_data['service_specific_credential']['service_user_name']
    service_password  = codecommit_data['service_specific_credential']['service_password']
    encoded_password  = URI.encode_www_form_component(service_password)

    url = nil
    if match = codecommit_uri.match(/^(https:\/\/)(.*)$/)
      url = "#{match[1]}#{service_user_name}:#{encoded_password}@#{match[2]}"
    end

    url
  else
    raise "Unable to find codecommit https url"
  end
end

#convert_to_module_ref_lock_format(dep_tree) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/client/command/install.rb', line 192

def convert_to_module_ref_lock_format(dep_tree)
  lock_format = {}

  dep_tree.each do |dep|
    dep_name = "#{dep[:namespace]}/#{dep[:name]}"

    full_dep = { dep_name => {} }

    if version = (dep[:version] || dep['version'])
      full_dep[dep_name].merge!({ 'version' => version })
    end

    if modules = (dep[:modules] || dep['modules'])
      full_dep[dep_name].merge!({ 'modules' => modules })
    end

    lock_format.merge!(full_dep)
  end

  lock_format
end

#installObject



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
# File 'lib/client/command/install.rb', line 33

def install
  FileUtils.mkdir_p(dtk_modules_gzip_location) unless Dir.exist?(dtk_modules_gzip_location)

  module_list = @dependency_tree.dup
  module_list << { namespace: @module_ref.namespace, name:@module_ref.name, version: @module_ref.version.str_version }

  modules_info = rest_get('modules/install', { module_list: module_list.to_json })
  main_module  = ret_main_module_install_info(modules_info)

  (modules_info || []).each do |module_info|
    dep_module_list = module_info['module_list']
    credentials = (module_info['credentails']||{})['credentials']

    raise Error.new('Unexpected that repoman did not return any credentials') unless credentials

    s3_args = Args.new({
      region:           'us-east-1',
      access_key_id:     credentials['access_key_id'],
      secret_access_key: credentials['secret_access_key'],
      session_token:     credentials['session_token']
    })
    storage = Storage.new(:s3, s3_args)

    dep_module_list.each do |module_info|
      if ModuleRef::Version.is_semantic_version?(module_info['version'])
        install_semantic_version(module_info, storage)
      else
        install_named_version(module_info)
      end
    end
  end

  install_main_module(main_module)
  @ret
end

#install_main_module(module_info) ⇒ Object



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
# File 'lib/client/command/install.rb', line 69

def install_main_module(module_info)
  credentials = module_info['credentials']
  s3_args = Args.new({
    region:           'us-east-1',
    access_key_id:     credentials['access_key_id'],
    secret_access_key: credentials['secret_access_key'],
    session_token:     credentials['session_token']
  })
  storage = Storage.new(:s3, s3_args)

  namespace, name = module_info['name'].split('/')
  module_location = nil

  unless @type == :dependency
    module_location = @explicit_path || "#{@module_directory}/#{name}"
  end

  if ModuleRef::Version.is_semantic_version?(module_info['version'])
    module_location = install_semantic_version(module_info, storage, module_location)
  else
    module_location = install_named_version(module_info, module_location)
  end

  ModuleDir.create_file_with_content("#{module_location}/#{DependencyTree::LOCK_FILE}", YAML.dump(convert_to_module_ref_lock_format(@dependency_tree)))
end

#install_named_version(module_info, target_location = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/client/command/install.rb', line 138

def install_named_version(module_info, target_location = nil)
  codecommit_uri   = construct_clone_url(module_info)
  install_location = target_location || "#{dtk_modules_location}/#{module_info['name']}-#{module_info['version']}"

  FileUtils.rm_rf(install_location) if Dir.exist?(install_location)

  GitClient.clone(codecommit_uri, install_location, module_info['version'])

  @ret << module_info.merge(location: install_location)
  print "Module installed in '#{install_location}'.\n"
  install_location
end

#install_semantic_version(module_info, storage, target_location = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/client/command/install.rb', line 114

def install_semantic_version(module_info, storage, target_location = nil)
  bucket, object_name = ret_s3_bucket_info(module_info)
  object_name_on_disk = object_name.gsub(/([\/])/,'__')
  object_location_on_disk = "#{dtk_modules_gzip_location}/#{object_name_on_disk}"
  download_args = Args.new({
    response_target: object_location_on_disk,
    bucket: bucket,
    key: object_name,
    response_content_encoding: "gzip"
  })
  storage.download(download_args)
  install_location = target_location || "#{dtk_modules_location}/#{module_info['name']}-#{module_info['version']}"

  FileUtils.rm_rf(install_location) if Dir.exist?(install_location)

  FileUtils.mkdir_p(install_location)
  `tar xC #{install_location} -f #{object_location_on_disk}`
  FileUtils.remove_entry(object_location_on_disk)

  @ret << module_info.merge(location: install_location)
  print "Module installed in '#{install_location}'.\n"
  install_location
end

#ret_main_module_install_info(modules_info = []) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/client/command/install.rb', line 95

def ret_main_module_install_info(modules_info = [])
  main_module_info = nil

  modules_info.each do |module_info|
    break if main_module_info

    module_list      = module_info['module_list'] || []
    main_module_info = module_list.find { |mod_info| mod_info['name'].eql?("#{@module_ref.namespace}/#{@module_ref.name}") && mod_info['version'].eql?(@module_ref.version.str_version) }

    if main_module_info
      module_list.delete(main_module_info)
      credentials = (module_info['credentails']||{})['credentials']
      main_module_info.merge!('credentials' => credentials)
    end
  end

  main_module_info
end

#ret_s3_bucket_info(module_info) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/client/command/install.rb', line 151

def ret_s3_bucket_info(module_info)
  bucket = nil
  object_name = nil

  if catalog_uri = module_info['uri']
    if match = catalog_uri.match(/.*amazonaws.com\/([^\/]*)\/(.*.gz)/)
      bucket = match[1]
      object_name = match[2]
    end
  else
    raise "Unexpected that publish response does not contain metadata!"
  end

  raise "Unable to extract bucket and/or object name data from catalog_uri!" if bucket.nil? || object_name.nil?

  return [bucket, object_name]
end