Statistics
| Branch: | Tag: | Revision:

root / daemons / ensure-dirs.in @ de5f8826

History | View | Annotate | Download (4.6 kB)

1
#!/bin/bash
2

    
3
set -e
4

    
5
LIBDIR="@LOCALSTATEDIR@/lib"
6
DATADIR="${LIBDIR}/ganeti"
7
RUNDIR="@LOCALSTATEDIR@/run"
8
GNTRUNDIR="${RUNDIR}/ganeti"
9
LOGDIR="@LOCALSTATEDIR@/log"
10
GNTLOGDIR="${LOGDIR}/ganeti"
11
LOCKDIR="@LOCALSTATEDIR@/lock"
12

    
13
_fileset_owner() {
14
  case "$1" in
15
    masterd)
16
      echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
17
      ;;
18
    confd)
19
      echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
20
      ;;
21
    rapi)
22
      echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
23
      ;;
24
    noded)
25
      echo "root:@GNTMASTERDGROUP@"
26
      ;;
27
    daemons)
28
      echo "@GNTMASTERUSER@:@GNTDAEMONSGROUP@"
29
      ;;
30
    masterd-confd)
31
      echo "@GNTMASTERUSER@:@GNTCONFDGROUP@"
32
      ;;
33
    *)
34
      echo "root:root"
35
      ;;
36
  esac
37
}
38

    
39
_ensure_file() {
40
  local file="$1"
41
  local perm="$2"
42
  local owner="$3"
43

    
44
  [[ -e "${file}" ]] || return 1
45
  chmod ${perm} "${file}"
46

    
47
  if ! [[ -z "${owner}" ]]; then
48
    chown ${owner} "${file}"
49
  fi
50

    
51
  return 0
52
}
53

    
54
_ensure_dir() {
55
  local dir="$1"
56
  local perm="$2"
57
  local owner="$3"
58

    
59
  [[ -d "${dir}" ]] || mkdir "${dir}"
60

    
61
  _ensure_file "${dir}" "${perm}" "${owner}"
62
}
63

    
64
_gather_files() {
65
  local path="$1"
66
  local perm="$2"
67
  local user="$3"
68
  local group="$4"
69

    
70
  shift 4
71

    
72
  find "${path}" -type f "(" "!" -perm ${perm} -or "(" "!" -user ${user} -or \
73
       "!" -group ${group} ")" ")" "$@"
74
}
75

    
76
_ensure_datadir() {
77
  local full_run="$1"
78

    
79
  _ensure_dir ${DATADIR} 0755 "$(_fileset_owner masterd)"
80
  _ensure_dir ${DATADIR}/queue 0700 "$(_fileset_owner masterd)"
81
  _ensure_dir ${DATADIR}/queue/archive 0700 "$(_fileset_owner masterd)"
82
  _ensure_dir ${DATADIR}/uidpool 0750 "$(_fileset_owner noded)"
83
  _ensure_dir ${DATADIR}/rapi 0750 "$(_fileset_owner rapi)"
84

    
85
  # We ignore these files if they don't exists (incomplete setup)
86
  _ensure_file ${DATADIR}/cluster-domain-secret 0640 \
87
               "$(_fileset_owner masterd)" || :
88
  _ensure_file ${DATADIR}/config.data 0640 "$(_fileset_owner masterd-confd)" || :
89
  _ensure_file ${DATADIR}/hmac.key 0440 "$(_fileset_owner confd)" || :
90
  _ensure_file ${DATADIR}/known_hosts 0644 "$(_fileset_owner masterd)" || :
91
  _ensure_file ${DATADIR}/rapi.pem 0440 "$(_fileset_owner rapi)" || :
92
  _ensure_file ${DATADIR}/rapi/users 0640 "$(_fileset_owner rapi)" || :
93
  _ensure_file ${DATADIR}/server.pem 0440 "$(_fileset_owner masterd)" || :
94
  _ensure_file ${DATADIR}/queue/serial 0600 "$(_fileset_owner masterd)" || :
95

    
96
  # To not change the utils.LockFile object
97
  touch ${DATADIR}/queue/lock
98
  _ensure_file ${DATADIR}/queue/lock 0600 "$(_fileset_owner masterd)"
99

    
100
  if ! [[ -z "${full_run}" ]]; then
101
    local queue_owner="$(_fileset_owner masterd)"
102
    local ssconf_owner="$(_fileset_owner noded)"
103

    
104
    find ${DATADIR}/queue/archive -type d | \
105
    while read path; do
106
      _ensure_dir ${path} 0700 "$queue_owner"
107
    done
108

    
109
    _gather_files ${DATADIR}/queue 0600 @GNTMASTERUSER@ @GNTMASTERDGROUP@ | \
110
    while read path; do
111
      _ensure_file "$path" 0600 "$queue_owner"
112
    done
113

    
114
    _gather_files ${DATADIR} 0600 root @GNTMASTERDGROUP@ -name 'ssconf_*' | \
115
    while read path; do
116
      _ensure_file "$path" 0444 "$ssconf_owner"
117
    done
118
  fi
119
}
120

    
121
_ensure_rundir() {
122
  _ensure_dir ${GNTRUNDIR} 0775 "$(_fileset_owner daemons)"
123
  _ensure_dir ${GNTRUNDIR}/socket 0750 "$(_fileset_owner daemons)"
124
  _ensure_dir ${GNTRUNDIR}/bdev-cache 0755 "$(_fileset_owner noded)"
125
  _ensure_dir ${GNTRUNDIR}/instance-disks 0755 "$(_fileset_owner noded)"
126
  _ensure_dir ${GNTRUNDIR}/crypto 0700 "$(_fileset_owner noded)"
127
  _ensure_dir ${GNTRUNDIR}/import-export 0755 "$(_fileset_owner noded)"
128

    
129
  # We ignore this file if it don't exists (not yet start up)
130
  _ensure_file ${GNTRUNDIR}/socket/ganeti-master 0770 \
131
               "$(_fileset_owner daemons)" || :
132
}
133

    
134
_ensure_logdir() {
135
  _ensure_dir ${GNTLOGDIR} 0770 "$(_fileset_owner daemons)"
136
  _ensure_dir ${GNTLOGDIR}/os 0750 "$(_fileset_owner daemons)"
137

    
138
  # We ignore these files if they don't exists (incomplete setup)
139
  _ensure_file ${GNTLOGDIR}/master-daemon.log 0600 "$(_fileset_owner masterd)" || :
140
  _ensure_file ${GNTLOGDIR}/conf-daemon.log 0600 "$(_fileset_owner confd)" || :
141
  _ensure_file ${GNTLOGDIR}/node-daemon.log 0600 "$(_fileset_owner noded)" || :
142
  _ensure_file ${GNTLOGDIR}/rapi-daemon.log 0600 "$(_fileset_owner rapi)" || :
143
}
144

    
145
_ensure_lockdir() {
146
  _ensure_dir ${LOCKDIR} 1777 ""
147
}
148

    
149
_operate_while_hold() {
150
  local fn=$1
151
  local path=$2
152
  shift 2
153

    
154
  (cd ${path};
155
   ${fn} "$@")
156
}
157

    
158
main() {
159
  local full_run
160

    
161
  while getopts "f" OPTION; do
162
    case ${OPTION} in
163
      f) full_run=1 ;;
164
    esac
165
  done
166

    
167
  _operate_while_hold "_ensure_datadir" ${DATADIR} ${full_run}
168
  _operate_while_hold "_ensure_rundir" ${RUNDIR}
169
  _operate_while_hold "_ensure_logdir" ${LOGDIR}
170
  _operate_while_hold "_ensure_lockdir" @LOCALSTATEDIR@
171
}
172

    
173
main "$@"