Class: ReleaseManager::SandboxCreateCli

Inherits:
Object
  • Object
show all
Defined in:
lib/release_manager/cli/sandbox_create_cli.rb

Class Method Summary collapse

Class Method Details

.add_defaults(key, options) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/release_manager/cli/sandbox_create_cli.rb', line 138

def self.add_defaults(key, options)
  if key == :modules
    defaults = (ENV['DEFAULT_MODULES'] || '').split(',').map(&:strip)
    ( options[:modules].to_a + defaults ).uniq
  elsif key == :default_members
    defaults = (ENV['DEFAULT_MEMBERS'] || '').split(',').map(&:strip)
    ( options[:default_members].to_a + defaults ).uniq
  end
end

.gitlab_serverObject



6
7
8
# File 'lib/release_manager/cli/sandbox_create_cli.rb', line 6

def self.gitlab_server
 ReleaseManager.gitlab_server
end

.runObject



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
# File 'lib/release_manager/cli/sandbox_create_cli.rb', line 10

def self.run
  options = {}
  OptionParser.new do |opts|
    opts.program_name = 'sandbox-create'
    opts.version = ReleaseManager::VERSION
    opts.on_head("\nSummary: Creates a new r10k sandbox by forking and creating a new branch on r10k-control,\n     creates a fork and branch for each module passed in, adds the upstream remote\n     for each module, updates the r10k-control Puppetfile to use those forks and branches\n     and pushes the branch to the upstream r10k-control.\n\n\nNote: If you already have any of these modules cloned, this script will attempt to update those modules\n  using git fetch and git checkout -b sandbox_name upstream/master.  So this should not destroy anything.\n\nConfiguration:\n\nThis script uses the following environment variables to automatically set some options, please ensure \nthey exist in your shell environment.  You can set an environment variable in the shell or define \nin your shell startup files.\n\nShell:  export VARIABLE_NAME=value\n\nR10K_REPO_URL            - The git repo url to r10k-control (ie. [email protected]:devops/r10k-control.git)\nGITLAB_API_ENDPOINT      - The api path to the gitlab server  (ie. https://gitlab_server/api/v4)\n                       replace gitlab_server with your server hostname\nGITLAB_API_PRIVATE_TOKEN - The gitlab user api token.  \n                       You can get a token here (\#{gitlab_server}/profile/personal_access_tokens, \n                       Ensure api box is checked.\nDEFAULT_MODULES          - The default set of modules to fork use when \n                       a sandbox (ie. export DEFAULT_MODULES='hieradata, roles')\n\nDEFAULT_MEMBERS          - The default members each forked project should add permissions\n                       to ( ie, DEFAULT_MEMBERS='ci_runner,r10k_user' )\n\nIf your gitlab server has a invalid certificate you can set the following variable to \"fix\" that trust issue.\nexport GITLAB_API_HTTPARTY_OPTIONS=\"{verify: false}\"\n\nExamples:\n  \#{opts.program_name} -n my_sandbox -m \"roles,profiles,developer\" \n  \#{opts.program_name} -n my_sandbox -m \"roles,profiles,developer\" --members=\"p1dksk2,devops,ci_runner\"\n  \#{opts.program_name} -n my_sandbox -s \"upstream/v0.5.0\" \n\nOptions:\n    EOF\n    )\n    opts.on('--members DEFAULT_MEMBERS', \"A comman seperated list of members to add to forked projects\") do |m|\n      options[:default_members] = m.split(',').map(&:strip)\n    end\n    opts.on('-n', \"--name NAME\", \"The name of your sandbox\") do |n|\n      options[:sandbox_name] = n\n    end\n    opts.on('-s', \"--src-target REMOTE/REF\", \"The source of the target to create your sandbox from, defaults to upstream/dev\") do |n|\n      options[:src_target] = n\n    end\n    opts.on('--control-url R10K_REPO_URL', \"git url to the r10k-control repo, defaults to R10K_CONTROL_URL env variable\") do |r|\n      options[:r10k_repo_url] = r\n    end\n    opts.on('-m', '--modules MODULES', \"A comma separated list of modules names from the Puppetfile to fork and branch\") do |c|\n      options[:modules] = c.split(',').map(&:strip)\n    end\n    opts.on('-r', '--repos-dir [REPOS_PATH]', \"The repos path to clone modules to. \" +\n        \"Defaults to: \#{File.expand_path(File.join(ENV['HOME'], 'repos'))}\") do |c|\n      options[:repos_path] = c\n    end\n    opts.on('-c', '--control-dir [CONTROL_DIR]', \"The r10k-control repo path. \" +\n        \"Defaults to: \#{File.expand_path(File.join(ENV['HOME'], 'repos', 'r10k-control'))}\") do |c|\n      options[:r10k_repo_path] = c\n    end\n    opts.on('--verbose', \"Extra logging\") do |c|\n      options[:verbose] = c\n    end\n  end.parse!\n  unless ENV['GITLAB_API_ENDPOINT']\n    puts \"Please set the GITLAB_API_ENDPOINT environment variable\".fatal\n    puts \"Example: export GITLAB_API_ENDPOINT=https://gitlab.com/api/v4\".fatal\n    exit 1\n  end\n\n  unless ENV['GITLAB_API_PRIVATE_TOKEN']\n    puts \"Please set the GITLAB_API_PRIVATE_TOKEN environment variable\".fatal\n    puts \"Example: export GITLAB_API_PRIVATE_TOKEN=kdii2ljljijsldjfoa\".fatal\n    exit 1\n  end\n\n  options[:modules] = add_defaults(:modules, options)\n  options[:default_members] = add_defaults(:default_members, options)\n\n  options[:r10k_repo_path] ||= File.expand_path(File.join(ENV['HOME'], 'repos', 'r10k-control'))\n  options[:repos_path] ||= File.expand_path(File.join(ENV['HOME'], 'repos'))\n  options[:r10k_repo_url] ||= ENV['R10K_REPO_URL']\n\n  options[:src_target] ||= 'upstream/dev'\n  if options[:src_target].split(\"/\").count < 2\n    puts \"Please use a source target that conforms to the remote/ref pattern\".fatal\n    exit 1\n  end\n  unless options[:r10k_repo_url]\n    puts \"Please set the R10K_REPO_URL environment variable or use the --control-url option\".fatal\n    puts \"Example: export R10K_REPO_URL='[email protected]:devops/r10k-control.git'\".fatal\n    exit 1\n  end\n  unless options[:sandbox_name]\n    puts \"If you don't name your sandbox, you will not have anywhere to play\".fatal\n    puts \"Example: sandbox-create -n my_sandbox\"\n    exit 1\n  end\n  unless options[:sandbox_name].length > 5\n    puts \"Your sandbox name must be at least 6 characters long\".fatal\n    puts \"Example: sandbox-create -n my_sandbox\"\n    exit 1\n  end\n  # check options to ensure all values are present\n  begin\n    ReleaseManager::VCSManager.default_instance.validate_authorization\n    s = Sandbox.create(options[:sandbox_name], options)\n  rescue Rugged::SshError => e\n    puts e.message.fatal\n    puts 'Please see https://github.com/nwops/release_manager#ssh-agent-usage for info on starting the ssh-agent'.fatal\n    exit 1\n  rescue InvalidToken => e\n    puts e.message.fatal\n    puts \"Please update your Gitlab API token as it may be expired or incorrect\".fatal\n    exit 1 \n  end\nend\n"