00001 #include <sys/types.h>
00002 #include <pwd.h>
00003 #include "jsoc_main.h"
00004
00005 char *module_name = "createns";
00006
00007 typedef enum
00008 {
00009 kCrnsErr_Success = 0,
00010 kCrnsErr_NsExists,
00011 kCrnsErr_DBQuery,
00012 kCrnsErr_Argument,
00013 kCrnsErr_FileIO
00014 } CrnsError_t;
00015
00016 #define kNamespace "ns"
00017 #define kNsGroup "nsgroup"
00018 #define kDbUser "dbusr"
00019 #define kFile "file"
00020 #define kNotSpec "NOTSPECIFIED"
00021
00022 ModuleArgs_t module_args[] =
00023 {
00024 {ARG_STRING, kNamespace, NULL, "DRMS namespace to create.", NULL},
00025 {ARG_STRING, kNsGroup, NULL, "DRMS namespace group in which new namespace will have membership.", NULL},
00026 {ARG_STRING, kDbUser, NULL, "DRMS database user that owns newly created namespace.", NULL},
00027 {ARG_STRING, kFile, kNotSpec, "Optional output file (output printed to stdout otherwise).", NULL},
00028 {ARG_END}
00029 };
00030
00031 static CrnsError_t CreateSQLMasterSeriesTable(FILE *fptr)
00032 {
00033 CrnsError_t err = kCrnsErr_Success;
00034
00035 fprintf(fptr,"CREATE TABLE %s (\n", DRMS_MASTER_SERIES_TABLE);
00036 fprintf(fptr," seriesname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXSERIESNAMELEN));
00037 fprintf(fptr," author %s NOT NULL,\n", db_stringtype_maxlen(255));
00038 fprintf(fptr," owner %s NOT NULL,\n", db_stringtype_maxlen(255));
00039 fprintf(fptr," unitsize %s NOT NULL,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00040 fprintf(fptr," archive %s NOT NULL,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00041 fprintf(fptr," retention %s NOT NULL,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00042 fprintf(fptr," tapegroup %s NOT NULL,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00043 fprintf(fptr," primary_idx %s NOT NULL,\n", db_stringtype_maxlen(1024));
00044 fprintf(fptr," created %s NOT NULL,\n", db_stringtype_maxlen(30));
00045 fprintf(fptr," description %s,\n", db_stringtype_maxlen(4000));
00046 fprintf(fptr," dbidx %s,\n", db_stringtype_maxlen(1024));
00047 fprintf(fptr," version %s,\n", db_stringtype_maxlen(1024));
00048 fprintf(fptr," PRIMARY KEY (seriesname));\n");
00049
00050 fprintf(fptr, "GRANT select ON %s TO public;\n", DRMS_MASTER_SERIES_TABLE);
00051
00052 return err;
00053 }
00054
00055 static CrnsError_t CreateSQLMasterKeywordTable(FILE *fptr)
00056 {
00057 CrnsError_t err = kCrnsErr_Success;
00058
00059 fprintf(fptr,"CREATE TABLE %s (\n", DRMS_MASTER_KEYWORD_TABLE);
00060 fprintf(fptr," seriesname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXSERIESNAMELEN));
00061 fprintf(fptr," keywordname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXKEYNAMELEN));
00062 fprintf(fptr," linkname %s ,\n", db_stringtype_maxlen(DRMS_MAXLINKNAMELEN));
00063 fprintf(fptr," targetkeyw %s ,\n", db_stringtype_maxlen(DRMS_MAXLINKNAMELEN));
00064 fprintf(fptr," type %s,\n", db_stringtype_maxlen(20));
00065 fprintf(fptr," defaultval %s ,\n", db_stringtype_maxlen(4000));
00066 fprintf(fptr," format %s ,\n", db_stringtype_maxlen(20));
00067 fprintf(fptr," unit %s ,\n", db_stringtype_maxlen(64));
00068 fprintf(fptr," islink %s default 0 ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00069 fprintf(fptr," isconstant %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00070 fprintf(fptr," persegment %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00071 fprintf(fptr," description %s ,\n", db_stringtype_maxlen(4000));
00072 fprintf(fptr," PRIMARY KEY (seriesname, keywordname));\n");
00073
00074 fprintf(fptr,"GRANT select ON %s TO public;\n", DRMS_MASTER_KEYWORD_TABLE);
00075
00076 return err;
00077 }
00078
00079 static CrnsError_t CreateSQLMasterLinkTable(FILE *fptr)
00080 {
00081 CrnsError_t err = kCrnsErr_Success;
00082
00083 fprintf(fptr,"CREATE TABLE %s (\n", DRMS_MASTER_LINK_TABLE);
00084 fprintf(fptr," seriesname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXSERIESNAMELEN));
00085 fprintf(fptr," linkname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXLINKNAMELEN));
00086 fprintf(fptr," target_seriesname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXSERIESNAMELEN));
00087 fprintf(fptr," type %s ,\n", db_stringtype_maxlen(20));
00088 fprintf(fptr," description %s ,\n", db_stringtype_maxlen(4000));
00089 fprintf(fptr," PRIMARY KEY (seriesname, linkname));\n");
00090
00091 fprintf(fptr,"GRANT select ON %s TO public;\n", DRMS_MASTER_LINK_TABLE);
00092
00093 return err;
00094 }
00095
00096 static CrnsError_t CreateSQLMasterSegmentTable(FILE *fptr)
00097 {
00098 CrnsError_t err = kCrnsErr_Success;
00099
00100 fprintf(fptr,"CREATE TABLE %s (\n", DRMS_MASTER_SEGMENT_TABLE);
00101 fprintf(fptr," seriesname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXSERIESNAMELEN));
00102 fprintf(fptr," segmentname %s NOT NULL,\n", db_stringtype_maxlen(DRMS_MAXSERIESNAMELEN));
00103 fprintf(fptr," segnum %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00104 fprintf(fptr," scope %s ,\n", db_stringtype_maxlen(10));
00105 fprintf(fptr," type %s,\n", db_stringtype_maxlen(20));
00106 fprintf(fptr," naxis %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00107 fprintf(fptr," axis %s ,\n", db_stringtype_maxlen(4000));
00108 fprintf(fptr," unit %s ,\n", db_stringtype_maxlen(64));
00109 fprintf(fptr," protocol %s ,\n", db_stringtype_maxlen(64));
00110 fprintf(fptr," description %s ,\n", db_stringtype_maxlen(4000));
00111 fprintf(fptr," islink %s default 0 ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00112 fprintf(fptr," linkname %s ,\n", db_stringtype_maxlen(DRMS_MAXLINKNAMELEN));
00113 fprintf(fptr," targetseg %s, \n", db_stringtype_maxlen(DRMS_MAXSEGNAMELEN));
00114 fprintf(fptr," cseg_recnum bigint default 0 ,\n");
00115 fprintf(fptr," PRIMARY KEY (seriesname, segmentname));\n");
00116
00117 fprintf(fptr,"GRANT select ON %s TO public;\n", DRMS_MASTER_SEGMENT_TABLE);
00118
00119 return err;
00120 }
00121
00122 static CrnsError_t CreateSQLMasterSessionTables(FILE *fptr)
00123 {
00124 CrnsError_t err = kCrnsErr_Success;
00125
00126 fprintf(fptr,"CREATE TABLE %s (\n", DRMS_SESSION_TABLE);
00127 fprintf(fptr," sessionid %s NOT NULL,\n", db_type_string(drms2dbtype(DRMS_TYPE_LONGLONG)));
00128 fprintf(fptr," hostname %s ,\n", db_stringtype_maxlen(30));
00129 fprintf(fptr," port %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00130 fprintf(fptr," pid %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00131 fprintf(fptr," sunum %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_LONGLONG)));
00132 fprintf(fptr," sudir %s ,\n", db_stringtype_maxlen(DRMS_MAXPATHLEN));
00133 fprintf(fptr," username %s ,\n", db_stringtype_maxlen(30));
00134 fprintf(fptr," starttime %s ,\n", db_stringtype_maxlen(30));
00135 fprintf(fptr," lastcontact %s ,\n", db_stringtype_maxlen(30));
00136 fprintf(fptr," endtime %s ,\n", db_stringtype_maxlen(30));
00137 fprintf(fptr," clients %s ,\n", db_type_string(drms2dbtype(DRMS_TYPE_INT)));
00138 fprintf(fptr," status %s ,\n", db_stringtype_maxlen(200));
00139 fprintf(fptr," sums_thread_status %s ,\n", db_stringtype_maxlen(200));
00140 fprintf(fptr," jsoc_version %s ,\n", db_stringtype_maxlen(200));
00141 fprintf(fptr," PRIMARY KEY (sessionid));\n");
00142
00143 fprintf(fptr,"GRANT select ON %s TO public;\n", DRMS_SESSION_TABLE);
00144 fprintf(fptr, "CREATE SEQUENCE drms_sessionid_seq;\n");
00145
00146 return err;
00147 }
00148
00149 static CrnsError_t CreateSQL(FILE *fptr, const char *ns, const char *nsgrp, const char *dbusr)
00150 {
00151 CrnsError_t err = kCrnsErr_Success;
00152
00153 fprintf(fptr, "CREATE SCHEMA %s;\n", ns);
00154
00155
00156
00157
00158
00159
00160 fprintf(fptr, "INSERT INTO admin.ns VALUES ('%s', '%s', '%s');\n", ns, nsgrp, dbusr);
00161 fprintf(fptr, "GRANT create, usage ON SCHEMA %s TO %s;\n", ns, dbusr);
00162 fprintf(fptr, "GRANT usage ON SCHEMA %s TO public;\n", ns);
00163
00164 fprintf(fptr, "SET search_path TO %s;\n", ns);
00165 fprintf(fptr, "SET ROLE %s;\n", dbusr);
00166
00167 err = CreateSQLMasterSeriesTable(fptr);
00168
00169 if (!err)
00170 {
00171 err = CreateSQLMasterKeywordTable(fptr);
00172 }
00173
00174 if (!err)
00175 {
00176 err = CreateSQLMasterLinkTable(fptr);
00177 }
00178
00179 if (!err)
00180 {
00181 err = CreateSQLMasterSegmentTable(fptr);
00182 }
00183
00184 if (!err)
00185 {
00186 err = CreateSQLMasterSessionTables(fptr);
00187 }
00188
00189 if (!err)
00190 {
00191 fprintf(fptr, "SET ROLE NONE;\n");
00192 fprintf(fptr, "SET search_path TO default;\n");
00193 }
00194
00195 return err;
00196 }
00197
00198 int DoIt(void)
00199 {
00200 CrnsError_t err = kCrnsErr_Success;
00201
00202 const char *ns = NULL;
00203 const char *nsgrp = NULL;
00204 const char *dbusr = NULL;
00205 const char *file = NULL;
00206 char *grp = NULL;
00207 FILE *fptr = NULL;
00208
00209 char query[DRMS_MAXQUERYLEN];
00210 DB_Text_Result_t *qres = NULL;
00211
00212
00213 if (!err)
00214 {
00215 ns = cmdparams_get_str(&cmdparams, kNamespace, NULL);
00216 if (!strcmp(ns, "public"))
00217 {
00218 fprintf(stderr, "Can't create DRMS master tables in namespace public.\n");
00219 err = kCrnsErr_Argument;
00220 }
00221 }
00222
00223
00224 if (!err)
00225 {
00226 nsgrp = cmdparams_get_str(&cmdparams, kNsGroup, NULL);
00227 grp = strdup(nsgrp);
00228 strtolower(grp);
00229 if (strcmp(grp, "user") != 0 && strcmp(grp, "sys") != 0)
00230 {
00231 fprintf(stderr, "Namespace group ('%s') must be either user or sys.\n", nsgrp);
00232 err = kCrnsErr_Argument;
00233 }
00234 }
00235
00236
00237 if (!err)
00238 {
00239 dbusr = cmdparams_get_str(&cmdparams, kDbUser, NULL);
00240
00241 if (strcmp(dbusr, kNotSpec) == 0)
00242 {
00243 struct passwd *pwd = getpwuid(geteuid());
00244 dbusr = pwd->pw_name;
00245 }
00246 }
00247
00248
00249 if (!err)
00250 {
00251 file = cmdparams_get_str(&cmdparams, kFile, NULL);
00252 if (strcmp(file, kNotSpec) == 0)
00253 {
00254 fptr = stdout;
00255 }
00256 else
00257 {
00258 fptr = fopen(file, "w");
00259 if (!fptr)
00260 {
00261 fprintf(stderr, "Unable to open file '%s'.\n", file);
00262 err = kCrnsErr_FileIO;
00263 }
00264 }
00265 }
00266
00267 if (!err)
00268 {
00269 err = CreateSQL(fptr, ns, grp, dbusr);
00270 }
00271
00272 if (!err)
00273 {
00274 fflush(fptr);
00275 }
00276
00277 if (grp)
00278 {
00279 free(grp);
00280 }
00281
00282 return err;
00283 }