-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmchp-install-pkg
executable file
·115 lines (96 loc) · 3.34 KB
/
mchp-install-pkg
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
#!/usr/bin/env ruby
# Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
# SPDX-License-Identifier: MIT
require 'optparse'
SERVER_INTERNAL = "herent-sw-pkg.microsemi.net"
SERVER_AMAZON = "mscc-ent-open-source.s3-website-eu-west-1.amazonaws.com"
SERVER_GITHUB = "github.com"
$opt = {
:server => nil,
:type => "mesa",
:install => "/opt/mscc",
}
global = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] package"
opts.on("-s", "--server <server>", "HTTP server (Default is internal server)") do |server|
$opt[:server] = server
end
opts.on("-t", "--type <type>", "Package type (Default is #{$opt[:type].to_s})") do |type|
if type.to_s != ""
$opt[:type] = type
else
raise ArgumentError, "Invalid type #{type}"
end
end
end.order!
def reachable server
%x(ping -q -c 1 #{server} > /dev/null 2>&1)
return $?.exitstatus == 0 ? true : nil
end
def url_get type, pkg
url = nil
package = pkg
if $opt[:server]
path = type # type must contain a full path if a server is specified
url = "http://#{$opt[:server]}/#{path}/#{package}.tar.gz"
elsif reachable(SERVER_INTERNAL) # get from local server if reachable
path = "jenkins/#{type}"
url = "http://#{SERVER_INTERNAL}/#{path}/#{package}.tar.gz"
else # Get package from external server
case type
when /^toolchains/ # Amazon
path = "public_root/toolchain"
url = "http://#{SERVER_AMAZON}/#{path}/#{package}.tar.gz"
when /^brsdk/ # Amazon
path = "public_root/bsp"
url = "http://#{SERVER_AMAZON}/#{path}/#{package}.tar.gz"
when /^frr/ # Amazon
path = "public_root/frr"
url = "http://#{SERVER_AMAZON}/#{path}/#{package}.tar.gz"
when /^mesa/ # GitHub
if package.match(/^mesa-(v\d{4}.\d{2})/)
path = "microchip-ung/mesa/releases/download/#{$1}"
package = "mesa-#{$1}" # cleanup package name
url = "http://#{SERVER_GITHUB}/#{path}/#{package}.tar.gz"
else
puts "Malformed package name: #{package}"
end
else
puts "Unknown type: #{type}"
end
end
return url, package
end
# Get exclusive lock
fh = File.open("#{$opt[:install]}/.mscc-install-pkg-lock", File::CREAT)
fh.flock(File::LOCK_EX)
ARGV.each do |p|
url, package = url_get($opt[:type], p)
exit 1 if url.nil?
tmp_dir = "#{$opt[:install]}/.mscc-install-pkg-tmp"
if File.directory?("#{$opt[:install]}/#{package}")
puts "#{package} seems to be installed already."
exit 0
end
%x(rm -rf #{tmp_dir})
puts tmp_dir
%x(mkdir -p #{tmp_dir})
puts "wget -q -O - #{url} | tar -C #{tmp_dir} -xzf -"
%x(wget -q -O - #{url} | tar -C #{tmp_dir} -xzf -)
if $?.exitstatus != 0
st = $?.exitstatus
%x(rm -rf #{tmp_dir})
puts "Package #{package} failed to download/install"
exit st
end
%x(mv #{tmp_dir}/#{package} #{$opt[:install]}/#{package})
if $?.exitstatus != 0
st = $?.exitstatus
%x(rm -rf #{tmp_dir})
%x(rm -rf #{$opt[:install]}/#{package})
puts "Failed to move downloaded packet into the #{$opt[:install]} directory"
exit st
end
%x(rm -rf #{tmp_dir})
puts "Package #{package} installed"
end