81 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/bin/bash
 | 
						|
 | 
						|
rname=$(date +%s | sha256sum | base64 | head -c 32)
 | 
						|
 | 
						|
if [ -z $GOGS_ROOT_URL ]
 | 
						|
then
 | 
						|
	printf "Need to set GOGS_ROOT_URL environment variable\n" >&2
 | 
						|
	printf "\tIE: https://git.cynarski.pl\n" >&2
 | 
						|
 | 
						|
    exit
 | 
						|
fi
 | 
						|
 | 
						|
if [ -z $GOGS_TOKEN ]
 | 
						|
then
 | 
						|
	printf "Need to set GOGS_TOKEN environment variable\n" >&2
 | 
						|
	printf "\tThis can be obtained at $GOGS_ROOT_URL/user/settings/applications\n" >&2
 | 
						|
	exit
 | 
						|
fi
 | 
						|
 | 
						|
if [ -z $GOGS_USER ]
 | 
						|
then
 | 
						|
	printf "Need to set GOGS_USER environment variable\n" >&2
 | 
						|
	exit
 | 
						|
fi
 | 
						|
 | 
						|
if [ -z $GOGS_REPO ]
 | 
						|
then
 | 
						|
    printf "Need to set GOGS_REPO environment variable\n" >&2
 | 
						|
    exit
 | 
						|
fi 
 | 
						|
 | 
						|
if [ -f $HOME/.ssh/gogs.pub ]; then
 | 
						|
    printf "Gogs deploy key is present\n" >&2
 | 
						|
else
 | 
						|
    mkdir -p $HOME/.ssh/
 | 
						|
    ssh-keygen -f $HOME/.ssh/gogs
 | 
						|
    chmod 400 $HOME/.ssh/gogs
 | 
						|
    chmod 400 $HOME/.ssh/gogs.pub
 | 
						|
fi
 | 
						|
 | 
						|
prefix="https://"
 | 
						|
sshhost=${GOGS_ROOT_URL#$prefix}
 | 
						|
 | 
						|
sbody=$(cat <<-END
 | 
						|
 | 
						|
Host $sshhost 
 | 
						|
    IdentityFile ~/.ssh/gogs.pub
 | 
						|
 | 
						|
END
 | 
						|
)
 | 
						|
 
 | 
						|
if [ -f $HOME/.ssh/$GOGS_REPO-sended ]; then
 | 
						|
    printf "Key sended\n" >&2
 | 
						|
else
 | 
						|
    printf "Prepare to send key\n" >&2
 | 
						|
    title=$rname@$(hostname)
 | 
						|
    printf "title $title\n" >&2
 | 
						|
    deploykey=$(cat $HOME/.ssh/gogs.pub)
 | 
						|
	body=$(cat <<-END
 | 
						|
		{
 | 
						|
			"title": "$title",
 | 
						|
			"key": "$deploykey"
 | 
						|
		}
 | 
						|
	END
 | 
						|
    )
 | 
						|
    prefix="https://"
 | 
						|
    sshhost=${GOGS_ROOT_URL#$prefix}
 | 
						|
 | 
						|
	curl -H "Content-Type: application/json" \
 | 
						|
		 -H "Authorization: token $GOGS_TOKEN" \
 | 
						|
		 -d "$body" \
 | 
						|
		 $GOGS_ROOT_URL/api/v1/repos/$GOGS_USER/$GOGS_REPO/keys > /dev/null
 | 
						|
    touch $HOME/.ssh/$GOGS_REPO-sended
 | 
						|
    echo "$sbody" >> $HOME/.ssh/config
 | 
						|
 | 
						|
    echo "Almost done. Enter clone repository target dir [ENTER]"
 | 
						|
    read cdir
 | 
						|
    git clone ssh://gogs@$sshhost:65522/$GOGS_USER/$GOGS_REPO $cdir
 | 
						|
fi
 | 
						|
 |