Merge ~chad.smith/cloud-init:tools/migrate-script into cloud-init:master
- Git
- lp:~chad.smith/cloud-init
- tools/migrate-script
- Merge into master
Status: | Merged |
---|---|
Approved by: | Chad Smith |
Approved revision: | 54cece7e182681987ee739849f5eb9aea7e5e35c |
Merge reported by: | Server Team CI bot |
Merged at revision: | not available |
Proposed branch: | ~chad.smith/cloud-init:tools/migrate-script |
Merge into: | cloud-init:master |
Diff against target: |
242 lines (+230/-0) 2 files modified
tools/.lp-to-git-user (+1/-0) tools/migrate-lp-user-to-github (+229/-0) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Server Team CI bot | continuous-integration | Approve | |
Ryan Harper | Needs Fixing | ||
Review via email: mp+375163@code.launchpad.net |
Commit message
tools: add migrate-
To link a launchpad account name to your github account for licensing
accountability each LP user should publish a merge proposal in launchpad
with their LP account and a matching merge proposal in github using
their github user.
Cloud-init will track these usename maps in ./tools/
JSON.
Run ./tools/
to automatically create merge proposals in launchpad and your github
account.
Description of the change
Server Team CI bot (server-team-bot) wrote : | # |
Server Team CI bot (server-team-bot) wrote : | # |
FAILED: Continuous integration, rev:8b499f40bdc
https:/
Executed test runs:
SUCCESS: Checkout
FAILED: Unit & Style Tests
Click here to trigger a rebuild:
https:/
Server Team CI bot (server-team-bot) wrote : | # |
PASSED: Continuous integration, rev:752e4d74a96
https:/
Executed test runs:
SUCCESS: Checkout
SUCCESS: Unit & Style Tests
SUCCESS: Ubuntu LTS: Build
SUCCESS: Ubuntu LTS: Integration
IN_PROGRESS: Declarative: Post Actions
Click here to trigger a rebuild:
https:/
Ryan Harper (raharper) wrote : | # |
This works quite well.
Can we add a check that the local master/upstream is in sync with github cloud-init master? I've not fetched from master, so the github pull request I got included my two usernames *and* the 19.3 release bump since my master local branch was behind remote master.
Ryan Harper (raharper) wrote : | # |
I re-ran, and the error message isn't clear about which remote (github or launchpad) has the existing branches.
So we should prefix each error with the host.
Also, it appears to be changing into a new branch under the covers, the migrate-
And I think we should emit the git commands used to reset things:
git push <remote> --delete migrate-
or have the script do this for users.
Ryan Harper (raharper) wrote : | # |
Also, I wish we'd store the Launchpad oath token so repeated invocations don't force a new browser open.
Ryan Harper (raharper) wrote : | # |
I can't get this to work after removing both branches.
Creating a migration branch: migrate-
The authorization page:
(https:/
should be opening in your browser. Use your browser to authorize
this program to access Launchpad on your behalf.
Waiting to hear from Launchpad about your decision...
[30816:
[30777:
Fontconfig error: Cannot load default config file
Traceback (most recent call last):
File "./tools/
File "./tools/
File "/usr/lib/
extra_
File "/usr/lib/
raise error
lazr.restfulcli
Response headers:
---
-content-encoding: gzip
connection: close
content-length: 155
content-
content-type: text/plain
date: Tue, 05 Nov 2019 23:56:18 GMT
server: zope.server.http (HTTP)
status: 400
strict-
vary: Accept,
x-content-
x-frame-options: SAMEORIGIN
x-launchpad-
x-lazr-
x-powered-by: Zope (www.zope.org), Python (www.python.org)
x-xss-protection: 1; mode=block
---
Response body:
---
b'There is already a branch merge proposal registered for branch ~raharper/
---
Server Team CI bot (server-team-bot) wrote : | # |
PASSED: Continuous integration, rev:bc9fa0fe5fb
https:/
Executed test runs:
SUCCESS: Checkout
SUCCESS: Unit & Style Tests
SUCCESS: Ubuntu LTS: Build
SUCCESS: Ubuntu LTS: Integration
IN_PROGRESS: Declarative: Post Actions
Click here to trigger a rebuild:
https:/
Server Team CI bot (server-team-bot) wrote : | # |
PASSED: Continuous integration, rev:7f6b69529fb
https:/
Executed test runs:
SUCCESS: Checkout
SUCCESS: Unit & Style Tests
SUCCESS: Ubuntu LTS: Build
SUCCESS: Ubuntu LTS: Integration
IN_PROGRESS: Declarative: Post Actions
Click here to trigger a rebuild:
https:/
Server Team CI bot (server-team-bot) wrote : | # |
PASSED: Continuous integration, rev:54cece7e182
https:/
Executed test runs:
SUCCESS: Checkout
SUCCESS: Unit & Style Tests
SUCCESS: Ubuntu LTS: Build
SUCCESS: Ubuntu LTS: Integration
IN_PROGRESS: Declarative: Post Actions
Click here to trigger a rebuild:
https:/
Preview Diff
1 | diff --git a/tools/.lp-to-git-user b/tools/.lp-to-git-user | |||
2 | 0 | new file mode 100644 | 0 | new file mode 100644 |
3 | index 0000000..0967ef4 | |||
4 | --- /dev/null | |||
5 | +++ b/tools/.lp-to-git-user | |||
6 | @@ -0,0 +1 @@ | |||
7 | 1 | {} | ||
8 | diff --git a/tools/migrate-lp-user-to-github b/tools/migrate-lp-user-to-github | |||
9 | 0 | new file mode 100755 | 2 | new file mode 100755 |
10 | index 0000000..6b095a1 | |||
11 | --- /dev/null | |||
12 | +++ b/tools/migrate-lp-user-to-github | |||
13 | @@ -0,0 +1,229 @@ | |||
14 | 1 | #!/usr/bin/python3 | ||
15 | 2 | """Link your Launchpad user to github, proposing branches to LP and Github""" | ||
16 | 3 | |||
17 | 4 | from argparse import ArgumentParser | ||
18 | 5 | from subprocess import Popen, PIPE | ||
19 | 6 | import os | ||
20 | 7 | import sys | ||
21 | 8 | |||
22 | 9 | try: | ||
23 | 10 | from launchpadlib.launchpad import Launchpad | ||
24 | 11 | except ImportError: | ||
25 | 12 | print("Missing python launchpadlib dependency to create branches for you." | ||
26 | 13 | "Install with: sudo apt-get install python3-launchpadlib" ) | ||
27 | 14 | sys.exit(1) | ||
28 | 15 | |||
29 | 16 | if "avoid-pep8-E402-import-not-top-of-file": | ||
30 | 17 | _tdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) | ||
31 | 18 | sys.path.insert(0, _tdir) | ||
32 | 19 | from cloudinit import util | ||
33 | 20 | |||
34 | 21 | |||
35 | 22 | DRYRUN = False | ||
36 | 23 | LP_TO_GIT_USER_FILE='.lp-to-git-user' | ||
37 | 24 | MIGRATE_BRANCH_NAME='migrate-lp-to-github' | ||
38 | 25 | GITHUB_PULL_URL='https://github.com/canonical/cloud-init/compare/master...{github_user}:{branch}' | ||
39 | 26 | GH_UPSTREAM_URL='https://github.com/canonical/cloud-init' | ||
40 | 27 | |||
41 | 28 | |||
42 | 29 | def error(message): | ||
43 | 30 | if isinstance(message, bytes): | ||
44 | 31 | message = message.decode('utf-8') | ||
45 | 32 | log('ERROR: {error}'.format(error=message)) | ||
46 | 33 | sys.exit(1) | ||
47 | 34 | |||
48 | 35 | |||
49 | 36 | def log(message): | ||
50 | 37 | print(message) | ||
51 | 38 | |||
52 | 39 | |||
53 | 40 | def subp(cmd, skip=False): | ||
54 | 41 | prefix = 'SKIPPED: ' if skip else '$ ' | ||
55 | 42 | log('{prefix}{command}'.format(prefix=prefix, command=' '.join(cmd))) | ||
56 | 43 | if skip: | ||
57 | 44 | return | ||
58 | 45 | proc = Popen(cmd, stdout=PIPE, stderr=PIPE) | ||
59 | 46 | out, err = proc.communicate() | ||
60 | 47 | if proc.returncode: | ||
61 | 48 | error(err if err else out) | ||
62 | 49 | return out.decode('utf-8') | ||
63 | 50 | |||
64 | 51 | |||
65 | 52 | LP_GIT_PATH_TMPL = 'git+ssh://{launchpad_user}@git.launchpad.net/' | ||
66 | 53 | LP_UPSTREAM_PATH_TMPL = LP_GIT_PATH_TMPL + 'cloud-init' | ||
67 | 54 | LP_REMOTE_PATH_TMPL = LP_GIT_PATH_TMPL + '~{launchpad_user}/cloud-init' | ||
68 | 55 | GITHUB_REMOTE_PATH_TMPL = 'git@github.com:{github_user}/cloud-init.git' | ||
69 | 56 | |||
70 | 57 | |||
71 | 58 | # Comment templates | ||
72 | 59 | COMMIT_MSG_TMPL = ''' | ||
73 | 60 | lp-to-git-users: adding {gh_username} | ||
74 | 61 | |||
75 | 62 | Mapped from {lp_username} | ||
76 | 63 | ''' | ||
77 | 64 | PUBLISH_DIR='/tmp/cloud-init-lp-to-github-migration' | ||
78 | 65 | |||
79 | 66 | def get_parser(): | ||
80 | 67 | parser = ArgumentParser(description=__doc__) | ||
81 | 68 | parser.add_argument( | ||
82 | 69 | '--dryrun', required=False, default=False, action='store_true', | ||
83 | 70 | help=('Run commands and review operation in dryrun mode, ' | ||
84 | 71 | 'making not changes.')) | ||
85 | 72 | parser.add_argument('launchpad_user', help='Your launchpad username.') | ||
86 | 73 | parser.add_argument('github_user', help='Your github username.') | ||
87 | 74 | parser.add_argument( | ||
88 | 75 | '--local-repo-dir', required=False, dest='repo_dir', | ||
89 | 76 | help=('The name of the local directory into which we clone.' | ||
90 | 77 | ' Default: {}'.format(PUBLISH_DIR))) | ||
91 | 78 | parser.add_argument( | ||
92 | 79 | '--upstream-branch', required=False, dest='upstream', | ||
93 | 80 | default='origin/master', | ||
94 | 81 | help=('The name of remote branch target into which we will merge.' | ||
95 | 82 | ' Default: origin/master')) | ||
96 | 83 | parser.add_argument( | ||
97 | 84 | '-v', '--verbose', required=False, default=False, action='store_true', | ||
98 | 85 | help=('Print all actions.')) | ||
99 | 86 | parser.add_argument( | ||
100 | 87 | '--push-remote', required=False, dest='pushremote', | ||
101 | 88 | help=('QA-only provide remote name into which you want to push')) | ||
102 | 89 | return parser | ||
103 | 90 | |||
104 | 91 | |||
105 | 92 | def create_publish_branch(upstream, publish_branch): | ||
106 | 93 | '''Create clean publish branch target in the current git repo.''' | ||
107 | 94 | branches = subp(['git', 'branch']) | ||
108 | 95 | upstream_remote, upstream_branch = upstream.split('/', 1) | ||
109 | 96 | subp(['git', 'checkout', upstream_branch]) | ||
110 | 97 | subp(['git', 'pull']) | ||
111 | 98 | if publish_branch in branches: | ||
112 | 99 | subp(['git', 'branch', '-D', publish_branch]) | ||
113 | 100 | subp(['git', 'checkout', upstream, '-b', publish_branch]) | ||
114 | 101 | |||
115 | 102 | |||
116 | 103 | def add_lp_and_github_remotes(lp_user, gh_user): | ||
117 | 104 | """Add lp and github remotes if not present. | ||
118 | 105 | |||
119 | 106 | @return Tuple with (lp_remote_name, gh_remote_name) | ||
120 | 107 | """ | ||
121 | 108 | lp_remote = LP_REMOTE_PATH_TMPL.format(launchpad_user=lp_user) | ||
122 | 109 | gh_remote = GITHUB_REMOTE_PATH_TMPL.format(github_user=gh_user) | ||
123 | 110 | remotes = subp(['git', 'remote', '-v']) | ||
124 | 111 | lp_remote_name = gh_remote_name = None | ||
125 | 112 | for remote in remotes.splitlines(): | ||
126 | 113 | if not remote: | ||
127 | 114 | continue | ||
128 | 115 | remote_name, remote_url, _operation = remote.split() | ||
129 | 116 | if lp_remote == remote_url: | ||
130 | 117 | lp_remote_name = remote_name | ||
131 | 118 | elif gh_remote == remote_url: | ||
132 | 119 | gh_remote_name = remote_name | ||
133 | 120 | if not lp_remote_name: | ||
134 | 121 | log("launchpad: Creating git remote launchpad-{} to point at your" | ||
135 | 122 | " LP repo".format(lp_user)) | ||
136 | 123 | lp_remote_name = 'launchpad-{}'.format(lp_user) | ||
137 | 124 | subp(['git', 'remote', 'add', lp_remote_name, lp_remote]) | ||
138 | 125 | subp(['git', 'fetch', lp_remote_name]) | ||
139 | 126 | if not gh_remote_name: | ||
140 | 127 | log("github: Creating git remote github-{} to point at your" | ||
141 | 128 | " GH repo".format(gh_user)) | ||
142 | 129 | gh_remote_name = 'github-{}'.format(gh_user) | ||
143 | 130 | subp(['git', 'remote', 'add', gh_remote_name, gh_remote]) | ||
144 | 131 | try: | ||
145 | 132 | subp(['git', 'fetch', gh_remote_name]) | ||
146 | 133 | except: | ||
147 | 134 | log("ERROR: [github] Could not fetch remote '{remote}'." | ||
148 | 135 | "Please create a fork for your github user by clicking 'Fork'" | ||
149 | 136 | " from {gh_upstream}".format( | ||
150 | 137 | remote=gh_remote, gh_upstream=GH_UPSTREAM_URL)) | ||
151 | 138 | sys.exit(1) | ||
152 | 139 | return (lp_remote_name, gh_remote_name) | ||
153 | 140 | |||
154 | 141 | |||
155 | 142 | def create_migration_branch( | ||
156 | 143 | branch_name, upstream, lp_user, gh_user, commit_msg): | ||
157 | 144 | """Create an LP to Github migration branch and add lp_user->gh_user.""" | ||
158 | 145 | log("Creating a migration branch: {} adding your users".format( | ||
159 | 146 | MIGRATE_BRANCH_NAME)) | ||
160 | 147 | create_publish_branch(upstream, MIGRATE_BRANCH_NAME) | ||
161 | 148 | lp_to_git_map = {} | ||
162 | 149 | lp_to_git_file = os.path.join(os.getcwd(), LP_TO_GIT_USER_FILE) | ||
163 | 150 | if os.path.exists(lp_to_git_file): | ||
164 | 151 | with open(lp_to_git_file) as stream: | ||
165 | 152 | lp_to_git_map = util.load_json(stream.read()) | ||
166 | 153 | if gh_user in lp_to_git_map.values(): | ||
167 | 154 | raise RuntimeError( | ||
168 | 155 | "github user '{}' already in {}".format(gh_user, lp_to_git_file)) | ||
169 | 156 | if lp_user in lp_to_git_map: | ||
170 | 157 | raise RuntimeError( | ||
171 | 158 | "launchpad user '{}' already in {}".format( | ||
172 | 159 | lp_user, lp_to_git_file)) | ||
173 | 160 | lp_to_git_map[lp_user] = gh_user | ||
174 | 161 | with open(lp_to_git_file, 'w') as stream: | ||
175 | 162 | stream.write(util.json_dumps(lp_to_git_map)) | ||
176 | 163 | subp(['git', 'add', lp_to_git_file]) | ||
177 | 164 | commit_file = os.path.join(os.path.dirname(os.getcwd()), 'commit.msg') | ||
178 | 165 | with open(commit_file, 'wb') as stream: | ||
179 | 166 | stream.write(commit_msg.encode('utf-8')) | ||
180 | 167 | subp(['git', 'commit', '--all', '-F', commit_file]) | ||
181 | 168 | |||
182 | 169 | |||
183 | 170 | def main(): | ||
184 | 171 | global DRYRUN | ||
185 | 172 | global VERBOSITY | ||
186 | 173 | parser = get_parser() | ||
187 | 174 | args = parser.parse_args() | ||
188 | 175 | DRYRUN = args.dryrun | ||
189 | 176 | VERBOSITY = 1 if args.verbose else 0 | ||
190 | 177 | repo_dir = args.repo_dir or PUBLISH_DIR | ||
191 | 178 | if not os.path.exists(repo_dir): | ||
192 | 179 | subp(['git', 'clone', | ||
193 | 180 | LP_UPSTREAM_PATH_TMPL.format(launchpad_user=args.launchpad_user), | ||
194 | 181 | repo_dir]) | ||
195 | 182 | os.chdir(repo_dir) | ||
196 | 183 | log("Sycing master branch with upstream") | ||
197 | 184 | subp(['git', 'checkout', 'master']) | ||
198 | 185 | subp(['git', 'pull']) | ||
199 | 186 | lp_remote_name, gh_remote_name = add_lp_and_github_remotes( | ||
200 | 187 | args.launchpad_user, args.github_user) | ||
201 | 188 | commit_msg = COMMIT_MSG_TMPL.format( | ||
202 | 189 | gh_username=args.github_user, lp_username=args.launchpad_user) | ||
203 | 190 | create_migration_branch( | ||
204 | 191 | MIGRATE_BRANCH_NAME, args.upstream, args.launchpad_user, | ||
205 | 192 | args.github_user, commit_msg) | ||
206 | 193 | |||
207 | 194 | for push_remote in (lp_remote_name, gh_remote_name): | ||
208 | 195 | subp(['git', 'push', push_remote, MIGRATE_BRANCH_NAME, '--force']) | ||
209 | 196 | |||
210 | 197 | # Make merge request on LP | ||
211 | 198 | log("[launchpad] Automatically creating merge proposal using launchpadlib") | ||
212 | 199 | lp = Launchpad.login_with( | ||
213 | 200 | "server-team github-migration tool", 'production', version='devel') | ||
214 | 201 | master = lp.git_repositories.getByPath( | ||
215 | 202 | path='cloud-init').getRefByPath(path='master') | ||
216 | 203 | LP_BRANCH_PATH='~{launchpad_user}/cloud-init/+git/cloud-init' | ||
217 | 204 | lp_git_repo = lp.git_repositories.getByPath( | ||
218 | 205 | path=LP_BRANCH_PATH.format(launchpad_user=args.launchpad_user)) | ||
219 | 206 | lp_user_migrate_branch = lp_git_repo.getRefByPath( | ||
220 | 207 | path='refs/heads/migrate-lp-to-github') | ||
221 | 208 | lp_merge_url = ( | ||
222 | 209 | 'https://code.launchpad.net/' + | ||
223 | 210 | LP_BRANCH_PATH.format(launchpad_user=args.launchpad_user) + | ||
224 | 211 | '/+ref/' + MIGRATE_BRANCH_NAME) | ||
225 | 212 | try: | ||
226 | 213 | lp_user_migrate_branch.createMergeProposal( | ||
227 | 214 | commit_message=commit_msg, merge_target=master, needs_review=True) | ||
228 | 215 | except Exception as e: | ||
229 | 216 | log('[launchpad] active merge proposal already exists at:\n' | ||
230 | 217 | '{url}\n'.format(url=lp_merge_url)) | ||
231 | 218 | else: | ||
232 | 219 | log("[launchpad] Merge proposal created at:\n{url}.\n".format( | ||
233 | 220 | url=lp_merge_url)) | ||
234 | 221 | log("To link your account to github open your browser and" | ||
235 | 222 | " click 'Create pull request' at the following URL:\n" | ||
236 | 223 | "{url}".format(url=GITHUB_PULL_URL.format( | ||
237 | 224 | github_user=args.github_user, branch=MIGRATE_BRANCH_NAME))) | ||
238 | 225 | return 0 | ||
239 | 226 | |||
240 | 227 | |||
241 | 228 | if __name__ == '__main__': | ||
242 | 229 | sys.exit(main()) |
FAILED: Continuous integration, rev:505ff34c3f0 d23249b2298b75c af760e61792701 /jenkins. ubuntu. com/server/ job/cloud- init-ci/ 1250/
https:/
Executed test runs:
SUCCESS: Checkout
FAILED: Unit & Style Tests
Click here to trigger a rebuild: /jenkins. ubuntu. com/server/ job/cloud- init-ci/ 1250//rebuild
https:/