use git from lambda
from __future__ import print_function
import os
import sys
import subprocess
import boto3
from botocore.client import Config
from dulwich.errors import (
SendPackError,
UpdateRefsError,
)
from dulwich.objectspec import (
parse_object,
parse_reftuples,
)
from contextlib import closing
from dulwich import porcelain
from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor
import dulwich
import dulwich.repo
import paramiko
import paramiko.client
s3 =boto3.client('s3', config=Config(signature_version='s3v4'))
class KeyParamikoSSHVendor(object):
def __init__(self):
self.ssh_kwargs = {'key_filename': '/tmp/id_rsa'}
def run_command(self, host, command, username=None, port=None,
progress_stderr=None):
if not isinstance(command, bytes):
raise TypeError(command)
if port is None:
port = 22
client = paramiko.SSHClient()
policy = paramiko.client.MissingHostKeyPolicy()
client.set_missing_host_key_policy(policy)
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, port=port,
**self.ssh_kwargs)
# Open SSH session
channel = client.get_transport().open_session()
# Run commands
channel.exec_command(command)
from dulwich.contrib.paramiko_vendor import (
_ParamikoWrapper as ParamikoWrapper)
return ParamikoWrapper(
client, channel, progress_stderr=progress_stderr)
def push(repo, remote_location, refspecs=None,
outstream=sys.stdout, errstream=sys.stderr):
with dulwich.porcelain.open_repo_closing(repo) as r:
client, path = dulwich.client.get_transport_and_path(remote_location)
selected_refs = []
def update_refs(refs):
selected_refs.extend(parse_reftuples(r.refs, refs, refspecs))
for (lh, rh, force) in selected_refs:
if lh is None:
del refs[rh]
else:
refs[rh] = r.refs[lh]
return refs
remote_location_bytes = remote_location.encode('UTF-8')
try:
client.send_pack(path, update_refs,
r.object_store.generate_pack_contents, progress=errstream.write)
errstream.write(b"Push to " + remote_location_bytes +
b" successful.\n")
except (UpdateRefsError, SendPackError) as e:
errstream.write(b"Push to " + remote_location_bytes +
b" failed -> " + e.message.encode('UTF-8') +
b"\n")
def lambda_handler(event, context):
print(sys.stderr)
print(getattr(sys.stderr, 'encoding', 'utf-8'))
s3.download_file('cs-test-data', 'temp-id_rsa', '/tmp/id_rsa')
cmd = "chmod 600 /tmp/id_rsa"
output = subprocess.check_output(cmd.split(" "))
print(output)
dulwich.client.get_ssh_vendor = KeyParamikoSSHVendor
github_repo = 'git@github.com:ijin/pygit.git'
new_branch = 'deploy_now'
repo = dulwich.porcelain.clone(github_repo, '/tmp/temp')
dulwich.porcelain.branch_create(repo, new_branch)
dulwich.porcelain.symbolic_ref(repo, new_branch)
customer_file = 'customer.txt'
customer_file_path = '/tmp/temp/' + customer_file
with open(customer_file_path, 'a') as f:
f.write('A')
dulwich.porcelain.add(repo, customer_file)
author = 'lambda <lambda@example.com>'
commit = dulwich.porcelain.commit(repo.path, message='customer: A', author=author, committer=author)
#dulwich.porcelain.push(repo, github_repo, ('refs/heads/' + new_branch))
push(repo, github_repo, ('refs/heads/' + new_branch))
cmd = "rm -rf /tmp/temp"
output = subprocess.check_output(cmd.split(" "))
print(output)
return commit