Class: Provider::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/depengine/provider/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepository

Returns a new instance of Repository.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/depengine/provider/repository.rb', line 14

def initialize
  @repository = ''
  @method     = ''
  @user       = ''
  @password   = ''
  @host       = ''
  @sshkey     = ''
  @svnbinary  = ''
  @svncmd     = ''
  @samba_mountpoints = ''
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/depengine/provider/repository.rb', line 8

def host
  @host
end

#methodObject

Returns the value of attribute method.



5
6
7
# File 'lib/depengine/provider/repository.rb', line 5

def method
  @method
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/depengine/provider/repository.rb', line 7

def password
  @password
end

#repositoryObject

Returns the value of attribute repository.



3
4
5
# File 'lib/depengine/provider/repository.rb', line 3

def repository
  @repository
end

#samba_mountpointsObject

Returns the value of attribute samba_mountpoints.



12
13
14
# File 'lib/depengine/provider/repository.rb', line 12

def samba_mountpoints
  @samba_mountpoints
end

#sshkeyObject

Returns the value of attribute sshkey.



9
10
11
# File 'lib/depengine/provider/repository.rb', line 9

def sshkey
  @sshkey
end

#svnbinaryObject

Returns the value of attribute svnbinary.



10
11
12
# File 'lib/depengine/provider/repository.rb', line 10

def svnbinary
  @svnbinary
end

#svncmdObject

Returns the value of attribute svncmd.



11
12
13
# File 'lib/depengine/provider/repository.rb', line 11

def svncmd
  @svncmd
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/depengine/provider/repository.rb', line 6

def user
  @user
end

#workerObject

Returns the value of attribute worker.



4
5
6
# File 'lib/depengine/provider/repository.rb', line 4

def worker
  @worker
end

Instance Method Details

#create_svn_cmd(svncmd, from, target) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/depengine/provider/repository.rb', line 173

def create_svn_cmd(svncmd, from, target)
  shell_cmd = svnbinary + ' ' + svncmd
  shell_cmd = shell_cmd + ' --force' if svncmd == 'checkout' or svncmd == 'export'
  shell_cmd = shell_cmd + ' --xml' if svncmd == 'info'

  if not user.empty? and not password.empty?
    shell_cmd = shell_cmd + ' --username ' + user + \
                                  ' --password ' + password
  end

  shell_cmd = shell_cmd + ' ' + repository + '/' + from
  shell_cmd = shell_cmd + ' ' + target.to_s if svncmd =~ /^checkout/ or svncmd =~ /^export/ # rubocop:disable Lint/UselessAssignment
end

#get_from_maven(from, to) ⇒ Object



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
# File 'lib/depengine/provider/repository.rb', line 26

def get_from_maven(from, to)
  response = nil
  req = nil
  target = to
  string_array = from.split('/')
  file_name = string_array[string_array.length - 1]
  target = target << '/' if target[target.length - 1, 1] != '/'
  target = target + file_name

  #### create target directory
  unless File.directory?(to)
    unless FileUtils.mkdir_p(to)
      $log.writer.error "Unable to create directory #{to}"
      exit 1
    end
  end

  url = URI.parse(repository + from)
  http = Net::HTTP.new(url.host, url.port)
  if url.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  begin
    http.start do |connection|
      req = Net::HTTP::Get.new(url.path)
      # req.basic_auth user, password
      response = connection.request(req)
      body = response.body

      case response
      when Net::HTTPSuccess
        # OK
        File.open(target, 'w') { |f| f.write body }
      else
        $log.writer.error 'Can not get file from ' + url.scheme + '://' + url.host + url.path
        exit 1
      end
    end
  rescue
    $log.writer.error "Can not make a HTTP connection to #{url.host}"
    exit 1
  end
end

#get_from_repository(from, to) ⇒ Object



166
167
168
169
170
171
# File 'lib/depengine/provider/repository.rb', line 166

def get_from_repository(from, to)
  get_from_maven(from, to) if method == 'maven'
  get_from_scp(from, to) if method == 'scp'
  get_from_svn(from, to) if method == 'svn'
  get_from_samba(from, to) if method == 'samba'
end

#get_from_samba(from, to) ⇒ Object



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
# File 'lib/depengine/provider/repository.rb', line 133

def get_from_samba(from, to)
  #### create target directory
  unless File.directory?(to)
    unless FileUtils.mkdir_p(to)
      $log.writer.error "Unable to create target directory #{to}"
      fail "Unable to create target directory #{to}"
    end
  end
  begin
    worker.samba_mount(repository, "#{samba_mountpoints}/#{repository}")

    # local rsync
    shell_cmd = "rsync -va --delete #{samba_mountpoints}/#{repository}/#{from}/ #{to}"
    $log.writer.debug "Execute command: #{shell_cmd}"
    output = system shell_cmd
    unless output
      $log.writer.error "#{method} #{shell_cmd} failed"
      fail "#{method} #{shell_cmd} failed"
    end
    $log.writer.info output

    # umount
    worker.samba_umount("#{samba_mountpoints}/#{repository}")

  rescue => e
    $log.writer.error "Can not provide files using #{method}"
    $log.writer.error e.message
    $log.writer.error e.backtrace.join("\n")
    worker.samba_umount("#{samba_mountpoints}/#{repository}")
    exit 1
  end
end

#get_from_scp(from, to) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/depengine/provider/repository.rb', line 72

def get_from_scp(from, to)
  unless File.file? sshkey
    $log.writer.error "Keyfile #{File.expand_path sshkey} does not exists"
    exit 1
  end

  #### create target directory
  unless File.directory?(to)
    unless FileUtils.mkdir_p(to)
      $log.writer.error "Unable to create directory #{to}"
      exit 1
    end
  end

  begin
    Net::SCP.start(host, user, keys: [sshkey], host_key: 'ssh-rsa', auth_methods: ['publickey']) do |scp|
      scp.download!(from, to, recursive: true)
    end
  rescue => e
    $log.writer.error "Can not copy #{host}:#{from} to #{to}"
    $log.writer.error e.message
    exit 1
  end
end

#get_from_svn(from, to) ⇒ Object



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
# File 'lib/depengine/provider/repository.rb', line 97

def get_from_svn(from, to)
  ### svn command not set
  if svncmd.empty?
    $log.writer.error 'No svn command defined'
    exit 1
  end

  ### get svn info (file or directory
  shell_cmd = create_svn_cmd('info', from, '')
  output = `#{shell_cmd}`
  doc = REXML::Document.new output
  root = doc.root
  target = to
  if root.elements['/info/entry'].attributes['kind'] == 'fil     e'
    string_array = from.split('/     ')
    file_name = string_array[string_array.length -      1]
    target = target << '/' if target[target.length - 1, 1] != '     /'
    target = target + file_name
  end

  #### create target directory
  unless File.directory?(to)
    unless FileUtils.mkdir_p(to)
      $log.writer.error "Unable to create directory #{to}"
      exit 1
    end
  end

  shell_cmd = create_svn_cmd(svncmd, from, target)
  $log.writer.debug "Execute command: #{shell_cmd}"
  output = system shell_cmd
  return if output
  $log.writer.error "svn #{svncmd} failed"
  fail "svn #{svncmd} failed"
end