Procházet zdrojové kódy

Add support for builtins packages

Bertrand Chenal před 7 roky
rodič
revize
ee1fe42172
8 změnil soubory, kde provedl 199 přidání a 11 odebrání
  1. 2 2
      README.md
  2. 9 4
      baker.py
  3. 59 0
      pkg/az.yaml
  4. 12 0
      pkg/git.yaml
  5. 82 0
      pkg/os.yaml
  6. 19 0
      pkg/pg.yaml
  7. 7 0
      pkg/py.yaml
  8. 9 5
      setup.py

+ 2 - 2
README.md

@@ -79,8 +79,8 @@ top level `env` directive of the config file and then command-line
 
 The command formatting use the string formatting tool from python, see
 [Python documentation](https://docs.python.org/3.4/library/string.html#format-examples)
-for examples.  In particular, you have to use a brace character (`{` or
-`}`) in a command, you can double it (`{{` or `}}`) to escape
+for examples.  In particular, you have to use a brace character (`{`
+or `}`) in a command, you can double it (`{{` or `}}`) to escape
 formatting.
 
 Each task can also declare extra key-value pairs in the `env`

+ 9 - 4
baker.py

@@ -31,7 +31,8 @@ log_handler.setLevel(logging.INFO)
 log_handler.setFormatter(logging.Formatter(log_fmt))
 logger.addHandler(log_handler)
 
-
+basedir, _ = os.path.split(__file__)
+PKG_DIR = os.path.join(basedir, 'pkg')
 TAB = '\n    '
 
 class BakerException(Exception):
@@ -293,6 +294,7 @@ class TaskGroup(Node):
 class LoadNode(Node):
     _children = {
         'file': Atom,
+        'pkg': Atom,
         'as': Atom,
     }
 
@@ -715,11 +717,14 @@ def load_cfg(path, prefix=None):
     if cfg.load:
         cfg_path = os.path.dirname(path)
         for item in cfg.load:
+            if item.get('file'):
+                child_prefix, _ = os.path.splitext(item.file)
+                child_path = os.path.join(cfg_path, item.file)
+            elif item.get('pkg'):
+                child_prefix, _ = os.path.splitext(item.pkg)
+                child_path = PKG_DIR
             if item.get('as'):
                 child_prefix = item['as']
-            else:
-                child_prefix, _ = os.path.splitext(item.file)
-            child_path = os.path.join(cfg_path, item.file)
             child_cfg = load_cfg(child_path, child_prefix.split('/'))
 
             for section in load_sections:

+ 59 - 0
pkg/az.yaml

@@ -0,0 +1,59 @@
+tasks:
+  # Creation, deletion and mgmt of VM
+  create-vm:
+     desc: Create a new azure VM
+     local: >
+       az vm create -n "bio-pm-prove-{vm_name}" -g "{ressource_group}"
+       --image UbuntuLTS  --admin-username deploy --ssh-key-value deploy_rsa.pub
+       --data-disk-sizes-gb 100 --public-ip-address "" --subnet "{subnet}"
+     once: true
+  delete-vm-only:
+     desc: Delete azure VM
+     local: az vm delete -n "bio-pm-prove-{vm_name}"  -g "RG-DC-BUS-QUANTS" -y
+     once: true
+  show-disk:
+     desc: Read disk id
+     local: >-
+       az vm show -g "RG-DC-BUS-QUANTS" --query "{query}"
+       -n "bio-pm-prove-{vm_name}"
+     once: true
+  delete-disk:
+     desc: Delete azure disk
+     local: az disk delete -n "{disk_name}" -g "RG-DC-BUS-QUANTS" -y
+     once: true
+  delete-vm:
+     desc: Delete both VM and attached disk
+     multi:
+      - task: az-show-disk
+        export: os_disk
+        env:
+          query: "storageProfile.osDisk.name"
+      - task: az-show-disk
+        export: data_disk
+        env:
+          query: "storageProfile.dataDisks[0].name"
+      - task: az-delete-vm-only
+      - task: az-delete-disk
+        env:
+          disk_name: "{data_disk}"
+      - task: az-delete-disk
+        env:
+          disk_name: "{os_disk}"
+  show-ip:
+     desc: Query azure vm by name for ip
+     local: >
+       az vm list-ip-addresses
+       --query "[?virtualMachine.name=='bio-pm-prove-{vm_name}']
+       .virtualMachine.network.privateIpAddresses[0]"
+     once: true
+  vm-info:
+     desc: Query azure vm by name for info
+     local: az vm list --query "[?name=='bio-pm-prove-{vm_name}']"
+     once: true
+  fix-hosts:
+    desc: "See: https://github.com/Microsoft/WSL/issues/491"
+    run: |
+      if grep -q $(hostname) /etc/hosts
+      then true
+      else sudo sed -i "s/127.0.0.1 localhost/127.0.0.1 localhost $(hostname)/g" /etc/hosts
+      fi

+ 12 - 0
pkg/git.yaml

@@ -0,0 +1,12 @@
+tasks:
+  clone:
+    desc: Clone git repo
+    run: |
+      if test ! -d {path}
+        then  git clone {repo_uri} {path}
+      fi
+  pull:
+    desc: Update codebase
+    run: cd src/prove && git pull
+    env:
+      ssh_user: prove

+ 82 - 0
pkg/os.yaml

@@ -0,0 +1,82 @@
+tasks:
+  # Generic tasks
+  bash:
+    desc: Interactive prompt
+    run: "bash"
+  create-dir:
+    desc: Add a new directory
+    run: "mkdir -p {path}"
+  symlink:
+    desc: Create symlink
+    run: |
+      if test ! -e {to}
+      then ln -s {from} {to}
+      fi
+  mount:
+    run: mount | grep ' {path} ' &> /dev/null || mount {path}
+  move:
+    desc: Move a file or directory
+    run: |
+      if test ! -e {to}
+      then mv {from} {to}
+      fi
+  copy:
+    desc: Copy a file or directory
+    run: |
+      if test -f {from}
+      then cp {from} {to}
+      else cp -Tr {from} {to}
+      fi
+  remove:
+    desc: Remove (-r) path
+    run: "rm -r {path}"
+  chown:
+    desc: Chown a file or directory
+    run: "chown -R {user}. {path}"
+  chmod:
+    desc: Chmod a file or directory
+    run: "chmod -R {mode} {path}"
+  apt-install:
+    desc: Run apt install
+    run: apt update && apt install -y {packages}
+    sudo: true
+  systemctl:
+    desc: Call systemctl
+    run: systemctl {action} {service}
+    sudo: true
+  send:
+    desc: Send a file or a directory
+    send: "{send}"
+    to: "{to}"
+  sudo-send:
+    desc: Combine send & sudo-move
+    multi:
+      - task: send
+        env:
+          to: "/tmp/{tmppath}"
+      - task: copy
+        sudo: true
+        env:
+          from: "/tmp/{tmppath}"
+      - task: remove
+        env:
+          path: "/tmp/{tmppath}"
+  parted:
+    desc: Create primary partition && mkfs
+    sudo: true
+    run: |
+      if test ! -e {device}1
+      then
+      parted {device} -a optimal --script mklabel gpt mkpart primary 0% 100%
+      sleep 1
+      mkfs -t ext4 {device}1
+      fi
+
+  append-line:
+    desc: Append line to file
+    run: >-
+      grep {guard} {file} &> /dev/null
+      || echo {line} >> {file}
+  add-user:
+    desc: Add a new user
+    run: id {user} || sudo adduser {user} --disabled-login --gecos ""

+ 19 - 0
pkg/pg.yaml

@@ -0,0 +1,19 @@
+tasks:
+  createuser:
+    desc: Add postgres user
+    sudo: postgres
+    run: |
+      if ! psql -c "SELECT usename FROM pg_user" | grep {pg_user} &> /dev/null
+      then createuser {pg_user} -d
+      fi
+  createdb:
+    desc: Create a new db
+    run: |
+      if ! psql -l | grep {db_name} &> /dev/null
+      then createdb {db_name}
+      fi
+
+  alter-passwd:
+    desc: Alter user password
+    sudo: postgres
+    run: psql -c "ALTER USER {pg_user} WITH PASSWORD '{pg_password}'"

+ 7 - 0
pkg/py.yaml

@@ -0,0 +1,7 @@
+tasks:
+  create-venv:
+    desc: Create python venv
+    run: |
+      if ! -e {venv_path}
+      then python3 -m venv {venv_path}
+      fi

+ 9 - 5
setup.py

@@ -1,17 +1,20 @@
 #!/usr/bin/env python
 from setuptools import setup
-
+from glob import glob
 import baker
 
 long_description = '''
-Baker is yet another deployment tool. Baker is a mashup of Fabric
-(http://www.fabfile.org/) and the sup config file layout
+
+Baker is yet another deployment tool. Baker is a mashup of Paramiko
+(https://www.paramiko.org/) and the sup config file layout
 (https://github.com/pressly/sup).
 
 The name Baker is a reference to Chet Baker.
 '''
 
-description = ('Simple deployment tool based on Fabric')
+description = ('Simple deployment tool based on Paramiko')
+
+pkg_yaml = glob('pkg/*.yaml')
 
 setup(name='Baker',
       version=baker.__version__,
@@ -27,4 +30,5 @@ setup(name='Baker',
               'bk = baker:main',
           ],
       },
-  )
+      data_files=[('pkg', pkg_yaml)],
+)