-
Notifications
You must be signed in to change notification settings - Fork 295
/
project.rb
1263 lines (1161 loc) · 29.9 KB
/
project.rb
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright 2012-2018 Chef Software, Inc.
# Copyright 2014 Noah Kantrowitz
#
# 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 "time" unless defined?(Time.zone_offset)
require "ffi_yajl" unless defined?(FFI_Yajl)
require "omnibus/manifest"
require "omnibus/manifest_entry"
require "omnibus/reports"
module Omnibus
class Project
class << self
#
# @param [String] name
# the name to the project definition to load from disk
#
# @return [Project]
#
def load(name, manifest = nil)
loaded_projects[name] ||= begin
filepath = Omnibus.project_path(name)
if filepath.nil?
raise MissingProject.new(name)
else
log.internal(log_key) do
"Loading project `#{name}' from `#{filepath}'."
end
end
instance = new(filepath, manifest)
instance.evaluate_file(filepath)
instance.load_dependencies
instance
end
end
#
# Reset cached project information.
#
def reset!
@loaded_projects = nil
end
private
#
# The list of projects loaded thus far.
#
# @return [Hash<String, Project>]
#
def loaded_projects
@loaded_projects ||= {}
end
end
attr_reader :manifest
include Cleanroom
include Digestable
include Logging
include NullArgumentable
include Sugarable
include Util
def initialize(filepath = nil, manifest = nil)
@filepath = filepath
@manifest = manifest
end
#
# @!group DSL methods
#
# The following DSL methods are available from within the project
# definitions.
# --------------------------------------------------
#
# **[Required]** Set or retrieve the name of the project.
#
# @example
# name 'chef'
#
# @raise [MissingRequiredAttribute] if a value was not set before being
# subsequently retrieved
#
# @param [String] val
# the name to set
#
# @return [String]
#
def name(val = NULL)
if null?(val)
@name || raise(MissingRequiredAttribute.new(self, :name, "hamlet"))
else
@name = val
end
end
expose :name
#
# Set or retrieve a friendly name for the project. This defaults to the
# capitalized name if not specified.
#
# @example
# friendly_name 'Chef'
#
# @param [String] val
# the name to set
#
# @return [String]
#
def friendly_name(val = NULL)
if null?(val)
@friendly_name || name.capitalize
else
@friendly_name = val
end
end
expose :friendly_name
#
# Set or retrieve the package name of the project. Defaults to the package
# name defaults to the project name.
#
# @example
# package_name 'com.chef.project'
#
# @param [String] val
# the package name to set
#
# @return [String]
#
def package_name(val = NULL)
if null?(val)
@package_name || name
else
@package_name = val
end
end
expose :package_name
#
# **[Required]** Set or retrieve the path at which the project should be
# installed by the generated package.
#
# Even on Windows-based systems, this path should be the Unix-like path,
# since that's what Ruby expects. In the event +\+ is used as a file
# separator, it will be replaced with +/+. This method also attempts to
# remove duplicate slashes which might be caused as a result of string
# interpolation.
#
# @example
# install_dir '/opt/chef'
#
# @raise [MissingRequiredAttribute] if a value was not set before being
# subsequently retrieved
#
# @param [String] val
# the install path to set
#
# @return [String]
#
def install_dir(val = NULL)
if null?(val)
@install_dir || raise(MissingRequiredAttribute.new(self, :install_dir, "/opt/chef"))
else
@install_dir = val.tr('\\', "/").squeeze("/").chomp("/") # rubocop:disable Style/StringLiterals
end
end
expose :install_dir
#
# The default root where a project should be installed. On Windows-based
# systems, this value defaults to +C:+. On non-Windows systems, this value
# defaults to +/opt+.
#
# @example
# install_dir "#{default_root}/chef" #=> Produces +C:/chef+ on Windows and
# #=> +/opt/chef+ on Linux
#
# @return [String]
#
def default_root
if windows?
"C:"
else
"/opt"
end
end
expose :default_root
#
# Path to the +/files+ directory in the omnibus project. This directory can
# contain arbitrary files used by the project.
#
# @example
# patch = File.join(files_path, 'rubygems', 'patch.rb')
#
# @return [String]
# path to the files directory
#
def files_path
File.expand_path("#{Config.project_root}/files")
end
expose :files_path
#
# **[Required]** Set or retrieve the the package maintainer.
#
# @example
# maintainer 'Chef Software, Inc.'
#
# @raise [MissingRequiredAttribute] if a value was not set before being
# subsequently retrieved
#
# @param [String] val
# the name of the maintainer
#
# @return [String]
#
def maintainer(val = NULL)
if null?(val)
@maintainer || raise(MissingRequiredAttribute.new(self, :maintainer, "Chef Software, Inc."))
else
@maintainer = val
end
end
expose :maintainer
#
# **[Required]** Set or retrive the package homepage.
#
# @example
# homepage 'https://www.getchef.com'
#
# @raise [MissingRequiredAttribute] if a value was not set before being
# subsequently retrieved
#
# @param [String] val
# the homepage for the project
#
# @return [String]
#
def homepage(val = NULL)
if null?(val)
@homepage || raise(MissingRequiredAttribute.new(self, :homepage, "https://www.getchef.com"))
else
@homepage = val
end
end
expose :homepage
#
# Set or retrieve the project description.
#
# @example
# description 'This is my description'
#
# @param [String] val
# the project description
#
# @return [String]
#
def description(val = NULL)
if null?(val)
@description || "The full stack of #{name}"
else
@description = val
end
end
expose :description
#
# Add to the list of packages this one replaces.
#
# This should only be used when renaming a package and obsoleting the old
# name of the package. **Setting this to the same name as package_name will
# cause RPM upgrades to fail.**
#
# @example
# replace 'the-old-package'
#
# @param [String] val
# the name of the package to replace
#
# @return [String]
#
def replace(val = NULL)
replaces << val
replaces.dup
end
expose :replace
#
# Add to the list of packages this one conflicts with.
#
# @example
# conflicts 'foo'
# conflicts 'bar'
#
# @param [String] val
# the conflict to add
#
# @return [Array<String>]
# the list of conflicts
#
def conflict(val)
conflicts << val
conflicts.dup
end
expose :conflict
#
# Set or retrieve the version of the project.
#
# @example Using a string
# build_version '1.0.0'
#
# @example From git
# build_version do
# source :git
# end
#
# @example From the version of a dependency
# build_version do
# source :version, from_dependency: 'chef'
# end
#
# @example From git of a dependency
# build_version do
# source :git, from_dependency: 'chef'
# end
#
# When using the +:git+ source, by default the output format of the
# +build_version+ is semver. This can be modified using the +:output_format+
# parameter to any of the methods of +BuildVersion+. For example:
#
# build version do
# source :git, from_dependency: 'chef'
# output_format :git_describe
# end
#
# @see Omnibus::BuildVersion
# @see Omnibus::BuildVersionDSL
#
# @param [String] val
# the build version to set
# @param [Proc] block
# the block to run when constructing the +build_version+
#
# @return [String]
#
def build_version(val = NULL, &block)
if block && !null?(val)
raise Error, "You cannot specify additional parameters to " \
"#build_version when a block is given!"
end
if block
@build_version_dsl = BuildVersionDSL.new(&block)
else
if null?(val)
@build_version_dsl.build_version
else
@build_version_dsl = BuildVersionDSL.new(val)
end
end
end
expose :build_version
#
# Set or retrieve the git revision of the omnibus
# project being built.
#
# If not set by the user, and the current workding directory is a
# git directory, it will return the revision of the current
# working directory.
#
def build_git_revision(val = NULL)
if null?(val)
@build_git_revision ||= get_local_revision
else
@build_git_revision = val
end
end
expose :build_git_revision
#
# Set or retrieve the build iteration of the project. Defaults to +1+ if not
# otherwise set.
#
# @example
# build_iteration 5
#
# @param [Fixnum] val
# the build iteration number
#
# @return [Fixnum]
#
def build_iteration(val = NULL)
if null?(val)
@build_iteration || 1
else
@build_iteration = val
end
end
expose :build_iteration
#
# Add or override a customization for the packager with the given +id+. When
# given multiple blocks with the same +id+, they are evaluated _in order_,
# so the last block evaluated will take precedence over the previous ones.
#
# @example
# package :id do
# key 'value'
# end
#
# @param [Symbol] id
# the id of the packager to customize
#
def package(id, &block)
unless block
raise InvalidValue.new(:package, "have a block")
end
packagers[id] << block
end
expose :package
#
# Add or override a customization for the compressor with the given +id+.
# When given multiple blocks with the same +id+, they are evaluated
# _in order_, so the last block evaluated will take precedence over the
# previous ones.
#
# @example With customization
# compress :dmg do
# window_bounds '10, 20, 30, 40'
# end
#
# @example Without customization
# compress :tgz
#
# If multiple +compress+ blocks are specified, the "most prefered" one for
# the current system will be used.
#
# @param [Symbol] id
# the id of the compressor to customize
#
def compress(id, &block)
if block
compressors[id] << block
else
compressors[id] << Proc.new {}
end
end
expose :compress
#
# Set or retrieve the user the package should install as. This varies with
# operating system, and may be ignored if the underlying packager does not
# support it.
#
# Defaults to +"root"+.
#
# @example
# package_user 'build'
#
# @param [String] val
# the user to use for the package build
#
# @return [String]
#
def package_user(val = NULL)
if null?(val)
@package_user || "root"
else
@package_user = val
end
end
expose :package_user
#
# Set or retrieve the overrides hash for one piece of software being
# overridden. Calling it as a setter does not merge hash entries and it will
# set all the overrides for a given software definition.
#
# @example
# override 'chef', version: '1.2.3'
#
# @param [Hash] val
# the value to override
#
# @return [Hash]
#
def override(name, val = NULL)
if null?(val)
overrides[name.to_sym]
else
overrides[name.to_sym] = val
end
end
expose :override
#
# Set or retrieve the group the package should install as. This varies with
# operating system and may be ignored if the underlying packager does not
# support it.
#
# Defaults to +Ohai['root_group']+. If +Ohai['root_group']+ is +nil+, it
# defaults to +"root"+.
#
# @example
# package_group 'build'
#
# @param [String] val
# the group to use for the package build
#
# @return [String]
#
def package_group(val = NULL)
if null?(val)
@package_group || Ohai["root_group"] || "root"
else
@package_group = val
end
end
expose :package_group
#
# Set or retrieve the path to the resources on disk for use in packagers.
#
# @example
# resources_path '/path/to/resources'
#
# @param [String] val
# the path where resources live
#
# @return [String]
#
def resources_path(val = NULL)
if null?(val)
@resources_path || "#{Config.project_root}/resources/#{name}"
else
@resources_path = File.expand_path(val)
end
end
expose :resources_path
#
# The path to the package scripts directory for this project. These are
# optional scripts that can be bundled into the resulting package for
# running at various points in the package management lifecycle.
#
# These scripts and their names vary with operating system.
#
# @return [String]
#
def package_scripts_path(arg = NULL)
if null?(arg)
@package_scripts_path || "#{Config.project_root}/package-scripts/#{name}"
else
@package_scripts_path = File.expand_path(arg)
end
end
expose :package_scripts_path
#
# Add a software dependency.
#
# Note that this is a *build time* dependency. If you need to specify an
# external dependency that is required at runtime, see {#runtime_dependency}
# instead.
#
# @example
# dependency 'foo'
# dependency 'bar'
#
# @param [String] val
# the name of a Software dependency
#
# @return [Array<String>]
# the list of dependencies
#
def dependency(val)
dependencies << val
dependencies.dup
end
expose :dependency
#
# Add a package that is a runtime dependency of this project.
#
# This is distinct from a build-time dependency, which should correspond to
# a software definition.
#
# @example
# runtime_dependency 'foo'
#
# @param [String] val
# the name of the runtime dependency
#
# @return [Array<String>]
# the list of runtime dependencies
#
def runtime_dependency(val)
runtime_dependencies << val
runtime_dependencies.dup
end
expose :runtime_dependency
#
# Add a new exclusion pattern for a list of files or folders to exclude
# when making the package.
#
# @example
# exclude '.git'
#
# @param [String] pattern
# the thing to exclude
#
# @return [Array<String>]
# the list of current exclusions
#
def exclude(pattern)
exclusions << pattern
exclusions.dup
end
expose :exclude
#
# Add a config file.
#
# @example
# config_file '/path/to/config.rb'
#
# @param [String] val
# the name of a config file of your software
#
# @return [Array<String>]
# the list of current config files
#
def config_file(val)
config_files << val
config_files.dup
end
expose :config_file
#
# Add other files or dirs outside of +install_dir+. These files retain their
# relative paths inside the scratch directory:
#
# /path/to/foo.txt #=> /tmp/package/path/to/foo.txt
#
#
# @example
# extra_package_file '/path/to/file'
#
# @param [String] val
# the name of a dir or file to include in build
#
# @return [Array<String>]
# the list of current extra package files
#
def extra_package_file(val)
extra_package_files << val
extra_package_files.dup
end
expose :extra_package_file
#
# A proxy method to the underlying Ohai system.
#
# @example
# ohai['platform_family']
#
# @return [Ohai]
#
def ohai
Ohai
end
expose :ohai
#
# Set or retrieve the {#license} of the project.
#
# @example
# license 'Apache 2.0'
#
# @param [String] val
# the license to set for the project.
#
# @return [String]
#
def license(val = NULL)
if null?(val)
@license || "Unspecified"
else
@license = val
end
end
expose :license
#
# Set or retrieve the location of the {#license_file}
# of the project. It can either be a relative path inside
# the project source directory or a URL.
#
#
# @example
# license_file 'LICENSES/artistic.txt'
#
# @param [String] val
# the location of the license file for the project.
#
# @return [String]
#
def license_file(val = NULL)
if null?(val)
@license_file
else
@license_file = val
end
end
expose :license_file
#
# Location of license file that omnibus will create and that will contain
# the information about the license of the project plus the details about
# the licenses of the software components included in the project.
#
# If no path is specified install_dir/LICENSE is used.
#
# @example
# license_file_path
#
# @return [String]
#
def license_file_path(path = NULL)
if null?(path)
@license_file_path || File.join(install_dir, "LICENSE")
else
@license_file_path = File.join(install_dir, path)
end
end
expose :license_file_path
#
# Location of json-formated version manifest, written at at the
# end of the build. If no path is specified
# +install_dir+/version-manifest.json is used.
#
# @example
# json_manifest_path
#
# @return [String]
#
def json_manifest_path(path = NULL)
if null?(path)
@json_manifest_path || File.join(install_dir, "version-manifest.json")
else
@json_manifest_path = path
end
end
expose :json_manifest_path
#
# Location of text-formatted manifest.
# (+install_dir+/version-manifest.txt if none is provided)
#
# This manifest uses the same format used by the
# 'version-manifest' software definition in omnibus-software.
#
# @example
# text_manifest_path
#
# @return [String]
#
def text_manifest_path(path = NULL)
if null?(path)
@text_manifest_path || File.join(install_dir, "version-manifest.txt")
else
@text_manifest_path = path
end
end
expose :text_manifest_path
#
# @!endgroup
# --------------------------------------------------
#
# @!group Public API
#
# In addition to the DSL methods, the following methods are considered to
# be the "public API" for a project.
# --------------------------------------------------
#
# Recursively load all the dependencies for this project.
#
# @return [true]
#
def load_dependencies
dependencies.each do |dependency|
Software.load(self, dependency, manifest)
end
true
end
#
# The list of software dependencies for this project. These is the software
# that comprises your project, and is distinct from runtime dependencies.
#
# @see #dependency
#
# @param [Array<String>]
#
# @return [Array<String>]
#
def dependencies
@dependencies ||= []
end
#
# The path (on disk) where this project came from. Warning: this can be
# +nil+ if a project was dynamically created!
#
# @return [String, nil]
#
def filepath
@filepath
end
#
#
# The list of config files for this software.
#
# @return [Array<String>]
#
def config_files
@config_files ||= []
end
#
# The list of files and directories used to build this project.
#
# @return [Array<String>]
#
def extra_package_files(val = NULL)
@extra_package_files ||= []
end
#
# The list of software dependencies for this project.
#
# These is the software that is used at runtime for your project.
#
# @return [Array<String>]
#
def runtime_dependencies
@runtime_dependencies ||= []
end
#
# The list of things this project conflicts with.
#
# @return [Array<String>]
#
def conflicts
@conflicts ||= []
end
#
# The list of things this project replaces with.
#
# @return [Array<String>]
#
def replaces
@replaces ||= []
end
#
# The list of exclusions for this project.
#
# @return [Array<String>]
#
def exclusions
@exclusions ||= []
end
#
# Retrieve the list of overrides for all software being overridden.
#
# @return [Hash]
#
def overrides
@overrides ||= {}
end
#
# The list of packagers, in the following format:
#
# {
# id: [#<Proc:0x001>, #<Proc:0x002>],
# # ...
# }
#
# @return [Hash<Symbol, Array<Proc>>]
# the packager blocks, indexed by key
#
def packagers
@packagers ||= Hash.new { |h, k| h[k] = [] }
end
#
# Instantiate new instances of the best packagers for this system.
#
# @return [[~Packager::Base]]
#
def packagers_for_system
@packagers_for_system ||= Packager.for_current_system.map { |p| p.new(self) }
end
#
# The list of compressors, in the following format:
#
# {
# id: [#<Proc:0x001>, #<Proc:0x002>],
# # ...
# }
#
# @return [Hash<Symbol, Array<Proc>>]
# the compressor blocks, indexed by key
#
def compressors
@compressors ||= Hash.new { |h, k| h[k] = [] }
end
#
# Instantiate a new instance of the best compressor for this system.
#
# @return [~Compressor::Base]
#
def compressor
@compressor ||= Compressor.for_current_system(compressors.keys).new(self)
end
#
# The DSL for this build version.
#
# @return [BuildVersionDSL]
#
def build_version_dsl
@build_version_dsl
end
#
# Indicates whether the given +software+ is defined as a software component
# of this project.
#
# @param [String, Software] software
# the software or name of the software to find
#
# @return [true, false]
#
def dependency?(software)
name = software.is_a?(Software) ? software.name : software
dependencies.include?(name)
end
#
# The library for this Omnibus project.
#
# @return [Library]
#
def library
@library ||= Library.new(self)
end
#
# Dirty the cache for this project. This can be called by other projects,
# install path cache, or software definitions to invalidate the cache for