-
Notifications
You must be signed in to change notification settings - Fork 2
/
createrole
executable file
·124 lines (107 loc) · 2.37 KB
/
createrole
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
#*******************************************************************************
# Name: createrole
# Description: Easily create ansible-galaxy roles in github
# Author: Mark Honomichl (mark at austincloud dot guru)
#*******************************************************************************
#****************
# Variables
#****************
authtoken=$GIT_TOKEN
#****************
# usage
#****************
usage()
{
echo -n "usage: $0 [OPTION]...
Script that easily creates an Ansible role in github
Options:
-n Name of the Ansible Role (required)
-g Name of the Github organization to create it in (required)
"
}
#****************
# getopts
#****************
while getopts ":pg:n:" opt; do
case $opt in
g)
gitaccount=$OPTARG
;;
n)
rolename=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Make sure rolname is set
if [ -z "$rolename" ]
then
usage
exit
fi
# Make sure gitaccount is set
if [ -z "$gitaccount" ]
then
usage
exit
fi
# Make sure molecule is available
if ! $(hash molecule &>/dev/null);
then
echo "Make sure you are in your ansible environment"
exit
fi
# Make sure GIT_TOKEN is set
if [ -z "$GIT_TOKEN" ]
then
echo "Make Sure you have your GIT_TOKEN set"
exit
fi
# Check to see if the Repo already exists
gitrepo=`curl -s -G https://api.github.com/search/repositories?q=$rolename+user:$gitaccount \
-H "Accept: application/vnd.github.preview" \
-H "Authorization: token $authtoken" \
|jq -r .items[].name`
# Exit if the repo already exists
if [ -n "$gitrepo" ]
then
echo "Repo Already Exists in Org!"
exit
fi
# Create the Git Repository
if [ $gitaccount == "austincloudguru" ]
then
git_endpoint=user/repos
else
git_endpoint=orgs/$gitaccount/repos
fi
giturl=$(curl -s -X POST https://api.github.com/$git_endpoint \
-H "Authorization: token $authtoken" \
-d @<(cat <<EOF
{
"name": "$rolename",
"private": "$privaterepo"
}
EOF
)|jq -r .ssh_url
)
# Create the Galaxy Role
molecule init role -r $rolename -d docker
cd $rolename
# Initialize the Git Repo
git init
git add .
git commit -m "Commit Skeleton Role"
# Add the Remote and Push
git remote add origin $giturl
git push -u origin master
cd ..
echo -e "\n\n\nCompleted setting up Ansible Galaxy Role"