From 769457e78a2e724da9b449d862ae32ac8fb243d4 Mon Sep 17 00:00:00 2001 From: Adam Saleh Date: Jan 03 2023 12:04:22 +0000 Subject: Merge pull request #83 from AdamSaleh/runner_explicit_sshkey Add explicit sshkey option to runner --- diff --git a/.github/workflows/centos-stream8.yml b/.github/workflows/centos-stream8.yml index 975fa22..4e627c3 100644 --- a/.github/workflows/centos-stream8.yml +++ b/.github/workflows/centos-stream8.yml @@ -8,12 +8,12 @@ jobs: uses: shimataro/ssh-key-action@v2 with: key: ${{ secrets.DUFFYY_SSH_PRIVATE_KEY }} - name: id_rsa # optional + name: id_centos_stream # optional known_hosts: NO - if_key_exists: fail # replace / ignore / fail; optional (defaults to fail) + if_key_exists: replace # replace / ignore / fail; optional (defaults to fail) - uses: "actions/checkout@v2" - run: sudo python3 -m pip install duffy fabric httpx - - run: python3 t_functional_duffy_runner/src/__main__.py --arch=x86 --release=centos-8s + - run: python3 t_functional_duffy_runner/src/__main__.py --arch=x86 --release=centos-9s --sshkey=~/.ssh/id_centos_stream env: DUFFY_AUTH_NAME: ${{ secrets.DUFFY_AUTH_NAME }} DUFFY_AUTH_KEY: ${{secrets.DUFFY_AUTH_KEY}} @@ -28,32 +28,10 @@ jobs: key: ${{ secrets.DUFFYY_SSH_PRIVATE_KEY }} name: id_rsa # optional known_hosts: NO - if_key_exists: fail # replace / ignore / fail; optional (defaults to fail) - - uses: "actions/checkout@v2" - - run: sudo python3 -m pip install duffy fabric httpx - - run: python3 t_functional_duffy_runner/src/__main__.py --arch=x86 --release=centos-9s - env: - DUFFY_AUTH_NAME: ${{ secrets.DUFFY_AUTH_NAME }} - DUFFY_AUTH_KEY: ${{secrets.DUFFY_AUTH_KEY}} - test_c9s_latest: - name: test_9s_latest - runs-on: ubuntu-latest - environment: centosduffy - steps: - - name: Install SSH key - uses: shimataro/ssh-key-action@v2 - with: - key: ${{ secrets.DUFFYY_SSH_PRIVATE_KEY }} - name: id_rsa # optional - known_hosts: NO - if_key_exists: fail # replace / ignore / fail; optional (defaults to fail) + if_key_exists: replace # replace / ignore / fail; optional (defaults to fail) - uses: "actions/checkout@v2" - run: sudo python3 -m pip install duffy fabric httpx - - run: python3 t_functional_duffy_runner/src/__main__.py --arch=x86 --release=centos-9s --compose=https://composes.stream.centos.org/development/latest-CentOS-Stream/compose/ + - run: python3 t_functional_duffy_runner/src/__main__.py --arch=x86 --release=centos-9s --sshkey=~/.ssh/id_centos_stream env: DUFFY_AUTH_NAME: ${{ secrets.DUFFY_AUTH_NAME }} DUFFY_AUTH_KEY: ${{secrets.DUFFY_AUTH_KEY}} - -name: Greeting -on: - push: {} diff --git a/t_functional_duffy_runner/src/__main__.py b/t_functional_duffy_runner/src/__main__.py index aeb1b83..b3e8b90 100644 --- a/t_functional_duffy_runner/src/__main__.py +++ b/t_functional_duffy_runner/src/__main__.py @@ -10,18 +10,18 @@ from invoke import run as local class DuffyWrapper: def __init__(self, auth_name, auth_key): self.c = DuffyClient(url="https://duffy.ci.centos.org/api/v1", auth_name=auth_name, auth_key=auth_key) - self.last_session = None - + self.last_session = None + def get_hostnames(self, *query): if not self.last_session: self.request_session(*query) - + nodes = [n for n in self.last_session.session.nodes if all(q in n.pool for q in query)] return [n for n in [n.data.get('provision',{}).get('public_hostname',None) for n in nodes] if n] - + def find_hostnames(self, *query): ls = self.c.list_sessions() nodes = [n for s in ls.sessions for n in s.nodes @@ -32,32 +32,38 @@ class DuffyWrapper: def find_pool_name(self, *query): return [p.name for p in self.c.list_pools().pools if all(q in p.name for q in query)] - + def request_session(self, *query): pool = self.find_pool_name(*query) session = self.c.request_session([{"pool":pool[0], "quantity":"1"}]) self.last_session = session return session - + class TmuxWrapper: - def __init__(self, host, session='default-session', **rest): + def __init__(self, host, session='default-session', private_key=None, **rest): self.host = host - self.c = Connection(host, **rest) + if private_key: + self.c = Connection(host,connect_kwargs={ + "key_filename": private_key, + }, **rest) + else: + self.c = Connection(host, **rest) + self.c.run("dnf -y install git tmux") self.ensure_session(session) - + def ensure_session(self,session='default-session'): return self.c.run("tmux has-session -t {session} || tmux new -s {session} -d".format(session=session)) - + def run(self, cmd, session='default-session'): return self.c.run("tmux send-keys -t {session}:0 '{cmd}' ENTER".format(session=session, cmd=cmd)) - + def run_and_notify(self, cmd, session='default-session'): return self.c.run("tmux send-keys -t {session}:0 '{cmd}; tmux wait-for -S {session}' ENTER".format(session=session, cmd=cmd)) def wait_for(self, session='default-session'): return self.c.run("tmux wait-for {session}".format(session=session)) - + def send_folder(self, path, remote): with tempfile.NamedTemporaryFile() as t: local("tar cf {to} -C {path} .".format(path=path, to=t.name)) @@ -95,10 +101,10 @@ enabled=1 self.c.run("dnf config-manager --enable baseos-compose") self.c.run("dnf config-manager --enable appstream-compose") self.c.run("dnf clean all") - -def runtests(auth_name, auth_key, query, path=None, compose=None): + +def runtests(auth_name, auth_key, query, path=None, compose=None, private_key=None): d = DuffyWrapper(auth_name=auth_name, auth_key=auth_key) print("Getting", *query) hostnames = d.get_hostnames(*query) @@ -107,7 +113,7 @@ def runtests(auth_name, auth_key, query, path=None, compose=None): hostname = hostnames[0] print("root@"+hostname) - tmux = TmuxWrapper("root@"+hostname) + tmux = TmuxWrapper("root@"+hostname, private_key=private_key) if compose: print("Setting compose", compose) tmux.set_repo(compose) @@ -130,12 +136,13 @@ def main(): parser.add_argument('--path', help="", default=None, required=False) parser.add_argument('--release', help="" ) parser.add_argument('--compose', help="", default=None, required=False) + parser.add_argument('--sshkey', help="", default=None, required=False) args = parser.parse_args() auth_name=os.getenv("DUFFY_AUTH_NAME") auth_key=os.getenv("DUFFY_AUTH_KEY") - print("Running with args:",args.arch, args.release, args.path, args.compose) + print("Running with args:",args.arch, args.release, args.path, args.compose, args.sshkey) if auth_name and auth_key: - runtests(auth_name, auth_key, ['virt', args.arch, args.release], path=args.path, compose=args.compose) + runtests(auth_name, auth_key, ['virt', args.arch, args.release], path=args.path, compose=args.compose, private_key=args.sshkey) else: raise Exception("Duffy key or auth name not available")