summaryrefslogtreecommitdiff
path: root/src/main/c/postshit/launch/openshift/ocexec.c
blob: 45c4af9861a3d8597563a1ac7668818f4c32cebc (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*

SH: true \
SH:   && `# Configure` \
SH:   && CC=x86_64-w64-mingw32-cc \
SH:   && MKDIR_P="mkdir -p" \
SH:   && CFLAGS="-Wall -Werror -pedantic -O0 -g -Isrc/main/c/common -DPROJECT_VERSION=0.0.0-$(date -u +%s) -fmax-errors=1 -Wno-error=unused-variable" \
SH:   && LDFLAGS="-Wl,--gc-sections,--as-needed" \
SH:   && `# Make` \
SH:   && ${MKDIR_P:?} build/bin \
SH:   && ${CC:?} -o build/bin/ocexec ${CFLAGS:?} src/main/c/postshit/launch/openshift/ocexec.c ${LDFLAGS:?} \
SH:   && true

*/

#include <assert.h>
#include <stdio.h>
#include <string.h>
#if __WIN32
#   include <windoof.h>
#endif

#define LOGERR(...) fprintf(stderr, __VA_ARGS__)
#if !NDEBUG
#   define REGISTER
#   define LOGDBG(...) fprintf(stderr, __VA_ARGS__)
#else
#   define REGISTER register
#   define LOGDBG(...)
#endif

#define FLG_isHelp (1<<0)


typedef  struct App  App;


struct App {
    int flg;
    char const *ocNamespace;
    char const *podName;
};


static void printHelp( void ){
    printf("  \n"
        "  TODO write help page\n"
        "  \n");
}


static int parseArgs( int argc, char**argv, App*app ){
    REGISTER int err;
    int iArg = 1;
    if( argc <= 1 ){ LOGERR("EINVAL: Luke.. use arguments!\n"); return-1; }
nextArg:;
    char const *arg = argv[iArg++];
    if( arg == NULL ) goto verifyArgs;
    if( !strcmp(arg,"--help") ){
        app->flg |= FLG_isHelp;
        //LOGDBG("[DEBUG] help -> true\n", arg);
        return 0;
    }else if( !strcmp(arg,"-n") || !strcmp(arg,"--namespace") ){
        arg = argv[iArg++];
        if( arg == NULL ){ LOGERR("EINVAL: %s needs value\n", argv[iArg-2]); return-1; }
        app->ocNamespace = arg;
        //LOGDBG("[DEBUG] namespace -> \"%s\"\n", arg);
    }else if( !strcmp(arg,"-p") || !strcmp(arg,"--pod") ){
        arg = argv[iArg++];
        if( arg == NULL ){ LOGERR("EINVAL: %s needs value\n", argv[iArg-2]); return-1; }
        app->podName = arg;
        //LOGDBG("[DEBUG] pod -> \"%s\"\n", arg);
    }else{
        LOGERR("EINVAL: %s\n", arg); return -1;
    }
    goto nextArg;
verifyArgs:
    return 0;
}


static int fetchPodnames( App*app ){
    assert(!"TODO_hCICALJrAgDwNgIAZ0ACAD9sAgB5UwIA");
    return -1;
}


static int resolvePodname( App*app ){
    REGISTER int err;
    err = fetchPodnames(app);
    if( err ) return err;
    if( !strcmp(app->podName, "houston") ){
    }
}


static int resolveNamespace( App*app ){
    if(0){
    }else if( !strcmp(app->ocNamespace,"test") ){
        app->ocNamespace = "isa-houston-test";
    }else if( !strcmp(app->ocNamespace,"int") ){
        app->ocNamespace = "isa-houston-int";
    }else if( !strcmp(app->ocNamespace,"preprod") ){
        app->ocNamespace = "isa-houston-preprod";
    }else{
        LOGDBG("[DEBUG] Use oc namespace as provided: \"%s\"\n", app->ocNamespace);
    }
    return 0;
}


static int run( App*app ){
    REGISTER int err;
    err = resolveNamespace(app); if( err ) return err;
    err = resolvePodname(app); if( err ) return err;

    LOGDBG("ENOTSUP: TODO continue here  %s:%d\n", __FILE__, __LINE__);

    PROCESS_INFORMATION proc;
    err = CreateProcessA(NULL, cmdline, NULL, NULL, !0, 0, NULL, NULL, &startInfo, &proc);
    if( err == 0 ){
        LOGERR("ERROR: CreateProcess(): 0x%0lX. %s:%d\n", GetLastError(), strrchr(__FILE__,'/')+1, __LINE__);
        err = -1; goto endFn; }
    err = WaitForSingleObject(proc.hProcess, INFINITE);
    if( err != WAIT_OBJECT_0 ){
        LOGERR("ERROR: WaitForSingleObject() -> %lu. %s:%d\n", GetLastError(), strrchr(__FILE__,'/')+1, __LINE__);
        err = -1; goto endFn; }
    long unsigned exitCode;
    err = GetExitCodeProcess(proc.hProcess, &exitCode);
    if( err == 0 ){
        LOGERR("ERROR: GetExitCodeProcess(): %lu. %s:%d\n", GetLastError(), strrchr(__FILE__,'/')+1, __LINE__);
        err = -1; goto endFn; }
    if( (exitCode & 0x7FFFFFFF) != exitCode ){
        LOGERR("EDOM: Exit code %lu out of bounds. %s:%d\n", exitCode, strrchr(__FILE__,'/')+1, __LINE__);
        err = -1; goto endFn;
    }
}


int main( int argc, char**argv ){
    REGISTER int err;
    App app = {0}; assert((void*)0 == NULL);
    #define app (&app)
    if( parseArgs(argc, argv, app) ){ err = -1; goto endFn; }
    LOGDBG("[DEBUG] flags are 0x%X\n", app->flg);
    if( app->flg & FLG_isHelp ){ printHelp(); err = 0; goto endFn; }
    err = run(app);
endFn:
    return !!err;
    #undef app
}