From 0f6532c0d561a99489b8d292f938c2c0fdea2fc4 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sun, 19 Oct 2014 00:08:28 +0100 Subject: [PATCH] Add cli and setup from file --- .gitignore | 1 + bin/.juju.swp | Bin 12288 -> 12288 bytes bin/juju | 124 +++++++++++++++++++++++++++++++------------------- lib/core.sh | 112 +++++++++++++++++++++++++++++++++++++++++++++ lib/util.sh | 37 +++++++++++++++ 5 files changed, 227 insertions(+), 47 deletions(-) create mode 100644 .gitignore create mode 100644 lib/core.sh create mode 100644 lib/util.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/bin/.juju.swp b/bin/.juju.swp index f0bb30527cf5f00611570316d00c500cc56130da..37af76d2cf0995b54a155ea883c87f7f7fc78724 100644 GIT binary patch literal 12288 zcmeI2TW=dh6vu}<c30OA`!;gu&qJXTzU;0<25JcB?;+=O@n@t;}SaiX9^LPA2?m44av&dix}{&Qw# zt4@99OfFByl1CY?`x#sO^%Nz|7+efez8lz^;bUFc>S1qVQH@A}NP+z;5co#o$$Q!3M`mYaqw&}nJ@`Ov|Lvoi zkphtdkphtdkphtdkphtdkphtd|9b`e_D$>sL~(OJlJouhO*`)WpV2K+AW|SwAW|Sw zAW|SwAW|SwAW|SwAW|SwAX4CesDRdB%)f=PTW^E&`2YXGzyE(c$k-3yI(QvC3!VWF zfKTpY>>Ahv0!)H$?`7;w&;fJp*mvMt@D=zBdLK1YQKkz!Tt`I~n^LTnDd!mq7>jC}-N16{BR7C{<31s(x^-Okvb;CJu?_!4{pJ_oOYGI$s~1a3gn_rbg1 z9dH#~0E-|E#sPsxfzs0XppJtVN+;tt7Vp(}hQ@4po z;4Ar>OS;o);n^e2<-|RY+rH2&j|?<89h?r5v?Aj+d5a9;dVVs-lxuK&jJl@K&CuSV z1xht|CuIkgMVBuR3uPapWo(?5Os;Pd48YK6m)l$sw&!b>#Tzs=>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#qs2?w{D(2;2w{1kfWAuTA(X?!*iTA-SY06nz4 z(Q#N4byve`KFG%H<3SpdL5`4T^2RQq8qzhd@7kQ~mUPX=xNm77BgR)&3ZAu`hA?_~ zp+f%mYo#)y)R#N7lpZ^lFQeO6=&x2w#7=xzfy-I(#qB*mJu> IbRDBSaGWfYHnqy=s)muQX-AZ9liE=^&YQ%ofARV? zSyyG_#+?&aCjNlkIPycBIDo_jxNu+sA#K`?3n#v|lQeW9zEU9lx*N$M5HP zY_-bUE?%uQ=v?6}!}bzmAAYpddgR_?53e%T<=jn9AGUeBmazMW^Q z&21T(+wpd6N)h`z*FEXd3pWl*Q=h& zfHI&ACdRRqd1kWk4BF29yD1Kp9X5 zlmTTx8Bhk40cGGBG+=cY`{FobKc2wN1$+($;62a)E8sFX3r>Q& zFEI8gaKIW^1tvHS{y4?h_uxBl4}1cAa0$E(-UQEqV_*g_@Zcn4-+-^dSKv!<7kmsl z;2Kx~b#NY>0KYxY*nRK=_zc_uL*Rl7;C1jCcoqDGx&8@$2lv6R;Aij?NIZNC)TRt5 z1ImChpbRJj%78NP{~Ev&(XC{$C|0F9jEER};+e;rDdI8?voU0!`^@Rm2F;SD=iG>l zi=@}r>+4(ZtTn26jc(wt$0Q7KyC0CjN!u)z%NwPI@;OgmOplA6#!i#II8R>Gqnu~? z9ZLpcH%|sdQp{6>N0HUzBm)YC<4e|!eOo$#PmbRWa{0Zf5L}3VL@7UoC<9CQPmWT0 z3Q=(9ftU(&(~(rl)Fmwt^j|G?9G;AuWB@^OdD_deq3Menl`hPkS)d_`GRu>`m!@y2 zwmLME)yRh37eOGWvbwK-Tsg_;R +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU Library General Public License as published +# by the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# -JUJU_HOME=$1 -[ -n $JUJU_HOME ] && JUJU_HOME=~/.juju -JUJU_REPO=https://bitbucket.org/fsquillace/juju-repo/raw/master -JUJU_BIN=$(dirname "$0") -ORIGIN_WD=$(pwd) +NAME='juju' +VERSION='1.0' +source "$(dirname $0)/../lib/core.sh" -function setup_juju(){ - trap - QUIT EXIT ABRT KILL TERM INT - trap "rm -rf ${maindir}; die \"Error occurred when installing $pkggrp\"" EXIT QUIT ABRT KILL TERM INT +################################### +### General functions ### +################################### - maindir=$(TMPDIR=/tmp mktemp -d -t juju.XXXXXXXXXX) - builtin cd ${maindir} +usage() { + echo -e "JuJu: The portable GNU/Linux distribution" + echo -e "Usage: $NAME [options]" + echo -e "Operations:" + echo -e "-i, --setup-from-file Setup the JuJu image ${JUJU_HOME}" + echo -e "-h, --help Show this help message" + echo -e "-v, --version Show the $NAME version" +} - info "Downloading JuJu..." - imagefile=juju-$(uname -m).tar.gz - wget ${JUJU_REPO}/$imagefile - tar -zxpf $imagefile - - info "Installing JuJu..." - mkdir -p $JUJU_HOME - cp -f -a ${maindir}/juju/* $JUJU_HOME - info "JuJu installed successfully" - - builtin cd $ORIGIN_WD - trap - QUIT EXIT ABRT KILL TERM INT +version() { + echo -e "JuJu ($VERSION): The portable GNU/Linux distribution" + echo -e "Copyright (c) 2012-2014 Filippo Squillace " + echo -e "Homepage: http://github.com/fsquillace/juju" } -function run_juju(){ - ${JUJU_BIN}/arch-chroot $JUJU_HOME -} +################################### +### MAIN PROGRAM ### +################################### +TEMP=`getopt -o ihv --long setup-from-file,help,version -n 'juju' -- "$@"` -echoerr() { echo "$@" 1>&2; } -function die(){ -# $@: msg (mandatory) - str: Message to print - error $@ - exit 1 -} -function error(){ -# $@: msg (mandatory) - str: Message to print - echoerr -e "\033[1;31m$@\033[0m" -} -function warn(){ -# $@: msg (mandatory) - str: Message to print - echoerr -e "\033[1;33m$@\033[0m" -} -function info(){ -# $@: msg (mandatory) - str: Message to print - echo -e "\033[1;37m$@\033[0m" -} +if [ $? != 0 ] ; then error "Error on parsing the command line. Try juju -h." ; exit ; fi -if [ ! "$(ls -A $JUJU_HOME)" ] +# Note the quotes around `$TEMP': they are essential! +eval set -- "$TEMP" + +OPT_SETUP_FROM_FILE=false +OPT_HELP=false +OPT_VERSION=false +while true ; do + case "$1" in + -i|--setup-from-file) OPT_SETUP_FROM_FILE=true ; shift ;; + -h|--help) OPT_HELP=true ; shift ;; + -v|--version) OPT_VERSION=true ; shift ;; + --) shift ; break ;; + *) error "Internal error!" ; exit 1 ;; + esac +done + +$OPT_HELP && usage && exit +$OPT_VERSION && version && exit + +ARGS=() +for arg do + ARGS+=($arg) +done + +############## CHECKING ESSENTIAL COMMANDS #################### +# Check if the essentials commands are installed (wget and tar) +if ! which wget &> /dev/null || ! which tar &> /dev/null then - setup_juju + error "Error: wget and tar commands are essentials for JuJu. Get them first." + exit 1 fi -run_juju - +################ DEFINE ACTION ######################## +if $OPT_SETUP_FROM_FILE; then + setup_from_file_juju ${ARGS[@]} +else + setup_and_run_juju +fi +# vim: set ts=4 sw=4 noet: diff --git a/lib/core.sh b/lib/core.sh new file mode 100644 index 0000000..d52ad03 --- /dev/null +++ b/lib/core.sh @@ -0,0 +1,112 @@ +#!/usr/bin/env bash +# +# This file is part of JuJu: The portable GNU/Linux distribution +# +# Copyright (c) 2012-2014 Filippo Squillace +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU Library General Public License as published +# by the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# References: +# https://wiki.archlinux.org/index.php/PKGBUILD +# https://wiki.archlinux.org/index.php/Creating_Packages + +################################ IMPORTS ################################# +# Define the variables for the dependency commands bash, wget, tar, which, awk, grep, xz, file +WGET=wget +TAR=tar +source "$(dirname ${BASH_ARGV[0]})/util.sh" + +################################# VARIABLES ############################## +[ -z ${JUJU_HOME} ] && JUJU_HOME=~/.juju +JUJU_REPO=https://bitbucket.org/fsquillace/juju-repo/raw/master +JUJU_BIN=$(dirname "$0") +ORIGIN_WD=$(pwd) + +################################# MAIN FUNCTIONS ############################## + + +function cleanup_build_directory(){ +# $1: maindir (optional) - str: build directory to get rid + local maindir=$1 + builtin cd $ORIGIN_WD + trap - QUIT EXIT ABRT KILL TERM INT + rm -fr "$maindir" +} + + +function prepare_build_directory(){ + trap - QUIT EXIT ABRT KILL TERM INT + trap "rm -rf ${maindir}; die \"Error occurred when installing JuJu\"" EXIT QUIT ABRT KILL TERM INT +} + + +function setup_juju(){ +# Setup the JuJu environment + local maindir=$(TMPDIR=/tmp mktemp -d -t juju.XXXXXXXXXX) + prepare_build_directory + + info "Downloading JuJu..." + builtin cd ${maindir} + local imagefile=juju-$(uname -m).tar.gz + wget ${JUJU_REPO}/${imagefile} + + info "Installing JuJu..." + mkdir -p ${JUJU_HOME} + builtin cd ${JUJU_HOME} + tar -zxpf ${maindir}/${imagefile} + info "JuJu installed successfully" + + cleanup_build_directory ${maindir} +} + + +function setup_from_file_juju(){ +# Setup from file the JuJu environment + if [ "$(ls -A $JUJU_HOME)" ] + then + error "Error: JuJu has been already installed in $JUJU_HOME" + return 1 + fi + + local imagefile=$1 + if [ ! -e ${imagefile} ] + then + die "Error: The JuJu image file ${imagefile} does not exist" + fi + info "Installing JuJu from ${imagefile}..." + mkdir -p ${JUJU_HOME} + builtin cd ${JUJU_HOME} + tar -zxpf ${ORIGIN_WD}/${imagefile} + info "JuJu installed successfully" + + builtin cd $ORIGIN_WD +} + + +function run_juju(){ + ${JUJU_BIN}/arch-chroot $JUJU_HOME +} + + +function setup_and_run_juju(){ +# Setup and run the JuJu environment +# The setup function will be executed only if the +# JuJu envinronment in $JUJU_HOME is not present. + + [ ! "$(ls -A $JUJU_HOME)" ] && setup_juju + run_juju +} + + diff --git a/lib/util.sh b/lib/util.sh new file mode 100644 index 0000000..8388fe1 --- /dev/null +++ b/lib/util.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# +# This file is part of JuJu: The portable GNU/Linux distribution +# +# Copyright (c) 2012-2014 Filippo Squillace +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU Library General Public License as published +# by the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +echoerr() { echo "$@" 1>&2; } +function die(){ +# $@: msg (mandatory) - str: Message to print + error $@ + exit 1 +} +function error(){ +# $@: msg (mandatory) - str: Message to print + echoerr -e "\033[1;31m$@\033[0m" +} +function warn(){ +# $@: msg (mandatory) - str: Message to print + echoerr -e "\033[1;33m$@\033[0m" +} +function info(){ +# $@: msg (mandatory) - str: Message to print + echo -e "\033[1;37m$@\033[0m" +}