summaryrefslogtreecommitdiffstats
path: root/tools/bcfg2-import-config
blob: fec007e7ede5eaed5c7e460fbf906b3d1466d5b7 (plain)
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
125
126
127
128
#!/bin/sh
#
# Import client configuration in to repository like tarball.
# This tarball can then be extracted on the server straight in to the server
# Repository. This makes it easier to import a live host in to bcfg2
#			- Sami Haahtinen <ressu@ressukka.net>
#
# TODO:
#  - Fetch filelist from server

usage() {
  echo "$0: tool to import files in to bcfg2-server repository"
  echo "	-s		Copy SSH Key files"
  echo "	-n		No suffix. Generate global files"
  echo "	--debian	Run debsums to detect changed configuration files"
  echo "			** debsums is only able to detect part of changes!"
  echo "	-h		Help (You are here)"
}

## Start Getopt
TEMP=`getopt -o snph --long help,debian -n $0 -- "$@"`

if [ $? != 0 ] ; then ( usage ) >&2 ; exit 1 ; fi

eval set -- "$TEMP"
## End Getopt

## Start Defaults
NEEDSSH=0
DEBSUMS=0
NOSUFFIX=0
# End Defaults

## Start option parse
while true ; do
  case "$1" in
    -s) NEEDSSH=1; shift ;;
    --debian) DEBSUMS=1; shift ;;
    -n) NOSUFFIX=1; shift ;;
    -h|--help)
      usage
      exit 0
      ;;
    --) shift; break ;;
    *)
      echo "Internal error!"
      exit 1
      ;;
  esac
done

FILES=$@
## End option parse

## Start functions
init_temp_repo() {
  TMPREPO=`mktemp -d`
  if [ $NEEDSSH -ne 0 ]; then
    SSHBASE="$TMPREPO/SSHbase"
    mkdir $SSHBASE
  fi
  CFGREPO="$TMPREPO/Cfg"
  HOSTNAME=`hostname -f`
  if [ $NOSUFFIX -eq 0 ]; then
    SUFFIX=".H_$HOSTNAME"
  else
    SUFFIX=""
  fi
}

package_temp_repo() {
  echo "Creating tarball to: /tmp/$HOSTNAME-bcfg2.tar.gz"
  # We should test for files here.
  tar -cz -C $TMPREPO -f /tmp/$HOSTNAME-bcfg2.tar.gz .
}

clean_temp_repo() {
  rm -r "$TMPREPO"
}

get_ssh() {
  if [ $NEEDSSH -ne 0 ]; then
    echo "Importing SSH host keys (if exists)"
    for i in $(find /etc/ssh -name ssh_host\*); do
      FILE=$(basename $i)
      cp $i $SSHBASE/${FILE}${SUFFIX}
    done
  fi
}

get_files() {
  if [ -n "$FILES" ]; then
    echo "Copying files:"

    # TODO: Files need an absolute path!
    for i in $FILES; do
      if [ -f $i ]; then
	echo -n "$i: "
	FILE=$(basename $i)
	mkdir -p $CFGREPO/$i
	cp $i $CFGREPO/$i/${FILE}${SUFFIX}
	echo "OK"
      else
	echo "$i: Not a file"
      fi
    done
  fi
}

get_debsums() {
  if [ $DEBSUMS -ne 0 ]; then
    echo "Locating changed configuration with debsums"
    echo " ** debsums by design is unable to find all changed files"
    echo "    you need to add missing files by hand."
    DEBSUMSFILES=$(debsums -ec 2> /dev/null)
    FILES="$FILES $DEBSUMSFILES"
  fi
}
## End Functions

if [ $(($NEEDSSH + $DEBSUMS)) -eq 0 -a -z "$FILES" ]; then usage ; exit 0; fi

init_temp_repo
get_debsums
get_ssh
get_files
package_temp_repo
clean_temp_repo