forked from chef/omnibus
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcli.rb
More file actions
192 lines (174 loc) · 6.8 KB
/
cli.rb
File metadata and controls
192 lines (174 loc) · 6.8 KB
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
189
190
191
192
#
# Copyright 2014 Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "thor"
require "omnibus"
require "ffi_yajl"
module Omnibus
class CLI < Command::Base
# This is the main entry point for the CLI. It exposes the method
# {#execute!} to start the CLI.
#
# @note the arity of {#initialize} and {#execute!} are extremely important
# for testing purposes. It is a requirement to perform in-process testing
# with Aruba. In process testing is much faster than spawning a new Ruby
# process for each test.
class Runner
include Logging
def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel)
@argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
end
def execute!
$stdin = @stdin
$stdout = @stdout
$stderr = @stderr
Omnibus::CLI.start(@argv)
@kernel.exit(0)
rescue Omnibus::Error => e
error = Omnibus.ui.set_color(e.message, :red)
backtrace = Omnibus.ui.set_color("\n" + e.backtrace.join("\n "), :red)
Omnibus.ui.error(error)
Omnibus.ui.error(backtrace)
if e.respond_to?(:status_code)
@kernel.exit(e.status_code)
else
@kernel.exit(1)
end
end
end
map %w{-v --version} => "version"
#
# Build an Omnibus project or software definition.
#
# $ omnibus build chefdk
#
method_option :output_manifest,
desc: "Create version-manifest.json in current directory at the end of the build",
type: :boolean,
default: true
method_option :use_manifest,
desc: "Use the given manifest when downloading software sources.",
type: :string,
default: nil
desc "build PROJECT", "Build the given Omnibus project"
def build(name)
manifest = if @options[:use_manifest]
Omnibus::Manifest.from_file(@options[:use_manifest])
else
nil
end
project = Project.load(name, manifest)
say("Building #{project.name} #{project.build_version}...")
# Not sure why we need the !Omnibus::S3Cache.fetch_missing.empty? check here
# We only want to populate the cache when we actually can. In some cases, we're only reading anonymously
# from the cache bucket (mostly when the build isn't ran on gitlab). When that is the case, we want to disable
# the cache population since it would fail.
Omnibus::S3Cache.populate if Omnibus::Config.use_s3_caching && Omnibus::Config.s3_authenticated_download && !Omnibus::S3Cache.fetch_missing.empty?
Omnibus::S3LicenseCache.populate if Omnibus::Config.use_s3_caching && Omnibus::Config.s3_authenticated_download
project.download
project.build
if @options[:output_manifest]
FileUtils.mkdir_p(Omnibus::Config.package_dir)
File.open(::File.join(Omnibus::Config.package_dir, "version-manifest.json"), "w") do |f|
f.write(FFI_Yajl::Encoder.encode(project.built_manifest.to_hash))
end
end
end
register(Command::ChangeLog, "changelog", "changelog [COMMAND]", "Create and view changelogs")
CLI.tasks["changelog"].options = Command::ChangeLog.class_options
#
# Generate a version manifest for the given project definition
#
# $ omnibus manifest PROJECT
#
method_option :os,
desc: "An os name in Ohai form. Defaults to the current os.",
type: :string
method_option :platform_family,
desc: "A platform family string in Ohai form. Defaults to the current platform_family.",
type: :string
method_option :platform,
desc: "A platform string in Ohai form. Defaults to the current platform.",
type: :string
method_option :platform_version,
desc: "A platform version string in Ohai form. Defaults to the current platform.",
type: :string
method_option :architecture,
desc: "The target architecture: x86, x64, powerpc. Defaults to the current architecture."
desc "manifest PROJECT", "Print a manifest for the given Omnibus project"
def manifest(name)
# Override ohai information
Ohai["os"] = @options[:os] if @options[:os]
Ohai["platform_family"] = @options[:platform_family] if @options[:platform_family]
Ohai["platform"] = @options[:platform] if @options[:platform]
Ohai["platform_version"] = @options[:platform_version] if @options[:platform_version]
Ohai["kernel"]["machine"] = @options[:architecture] if @options[:architecture]
puts FFI_Yajl::Encoder.encode(Project.load(name).built_manifest.to_hash, pretty: true)
end
#
# Perform cache management functions.
#
# $ omnibus cache list
#
register(Command::Cache, "cache", "cache [COMMAND]", "Manage the cache")
CLI.tasks["cache"].options = Command::Cache.class_options
#
# Clean the Omnibus project.
#
# $ omnibus clean chefdk
#
register(Cleaner, "clean", "clean PROJECT", "Clean the Omnibus project")
CLI.tasks["clean"].options = Cleaner.class_options
#
# Initialize a new Omnibus project.
#
# $ omnibus new NAME
#
register(Generator, "new", "new NAME", "Initialize a new Omnibus project")
CLI.tasks["new"].options = Generator.class_options
#
# List the Omnibus projects available from "here".
#
# $ omnibus list
#
desc "list", "List the Omnibus projects"
def list
if Omnibus.projects.empty?
say("There are no Omnibus projects!")
else
say("Omnibus projects:")
Omnibus.projects.sort.each do |project|
say(" * #{project.name} (#{project.build_version})")
end
end
end
#
# Publish Omnibus package(s) to a backend.
#
# $ omnibus publish s3 pkg/*chef*
#
register(Command::Publish, "publish", "publish [COMMAND]", "Publish Omnibus packages to a backend")
CLI.tasks["publish"].options = Command::Publish.class_options
#
# Display version information.
#
# $ omnibus version
#
desc "version", "Display version information", hide: true
def version
say("Omnibus v#{Omnibus::VERSION}")
end
end
end