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.



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

def initialize()
  repository = ""
  method     = ""
  user       = ""
  password   = ""
  host       = ""
  sshkey     = ""
  svnbinary  = ""
  svncmd     = ""
  samba_mountpoints = ""
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#repositoryObject

Returns the value of attribute repository.



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

def repository
  @repository
end

#samba_mountpointsObject

Returns the value of attribute samba_mountpoints.



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

def samba_mountpoints
  @samba_mountpoints
end

#sshkeyObject

Returns the value of attribute sshkey.



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

def sshkey
  @sshkey
end

#svnbinaryObject

Returns the value of attribute svnbinary.



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

def svnbinary
  @svnbinary
end

#svncmdObject

Returns the value of attribute svncmd.



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

def svncmd
  @svncmd
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

#workerObject

Returns the value of attribute worker.



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

def worker
  @worker
end

Instance Method Details

#create_svn_cmd(svncmd, from, target) ⇒ Object

create svn command



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/depengine/provider/repository.rb', line 182

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
  if svncmd =~ /^checkout/ or svncmd =~ /^export/
    shell_cmd = shell_cmd + " " + target.to_s
  end
  return shell_cmd
end

#get_from_repository(from, to) ⇒ Object



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

def get_from_repository(from, to)


   ### get from maven
  if method == "maven"
    res = nil
    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
     if not File.directory?(to)
       if not 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 |http|
    	  req = Net::HTTP::Get.new(url.path)
    	  #req.basic_auth user, password
         response = http.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 Exception => e
       $log.writer.error "Can not make a HTTP connection to #{url.host}"
       exit 1
     end
   end

   ### get via scp
  if method == "scp"

    if not File.file? sshkey
      $log.writer.error "Keyfile #{File.expand_path sshkey} does not exists"
      exit 1
    end

     #### create target directory
     if not File.directory?(to)
       if not 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 Exception => e
      $log.writer.error "Can not copy #{host}:#{from} to #{to}"
      $log.writer.error e.message
      exit 1
    end
  end

     ### get from svn
     if method == "svn"

     ### 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 = %x[#{shell_cmd}]
     doc = REXML::Document.new output
     root = doc.root
     target = to
if root.elements["/info/entry"].attributes["kind"] == "file"
 	  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
     if not File.directory?(to)
       if not 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
     if not output
       $log.writer.error "svn #{svncmd} failed"
       exit 1
     end

  end
     if method == "samba"
       #### create target directory
       if not File.directory?(to)
         if not FileUtils.mkdir_p(to)
           $log.writer.error "Unable to create target directory #{to}"
           exit 1
         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
         if not output
           $log.writer.error "#{method} #{shell_cmd} failed"
           exit 1
         end
         $log.writer.info output

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

       rescue Exception => 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
end