forked from smithjd/sql-pet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
920-environment-diagram.Rmd
168 lines (133 loc) · 5.99 KB
/
920-environment-diagram.Rmd
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Mapping your local environment diagram {#chapter_environment-diagram}
```{r setup, echo=FALSE, message=FALSE, warning=FALSE}
# These packages are called in almost every chapter of the book:
library(tidyverse)
library(DBI)
library(RPostgres)
library(glue)
library(here)
require(knitr)
library(dbplyr)
library(sqlpetr)
```
## Environment Tools Used in this Chapter
Note that `tidyverse`, `DBI`, `RPostgres`, `glue`, and `knitr` are loaded. Also, we've sourced the `[db-login-batch-code.R]('r-database-docker/book-src/db-login-batch-code.R')` file which is used to log in to PostgreSQL.
library(rstudioapi)
The following code block defines Tool and versions for the graph that follows. The information order corresponds to the order shown in the graph.
```{r eval=FALSE, echo=TRUE}
library(DiagrammeR)
## OS information
os_lbl <- .Platform$OS.type
os_ver <- 0
if (os_lbl == 'windows') {
os_ver <- system2('cmd',stdout = TRUE) %>%
grep(x = .,pattern = 'Microsoft Windows \\[',value = TRUE) %>%
gsub(x = .,pattern = "^Microsoft.+Version |\\]", replace = '')
}
if (os_lbl == 'unix' || os_lbl == 'Linux' || os_lbl == 'Mac') {
os_ver <- system2('uname', '-r', stdout = TRUE)
}
## Command line interface into Docker Apps
## CLI/system2
cli <- array(dim = 3)
cli[1] <- "docker [OPTIONS] COMMAND ARGUMENTS\n\nsystem2(docker,[OPTIONS,]\n, COMMAND,ARGUMENTS)"
cli[2] <- 'docker exec -it sql-pet bash\n\nsystem2(docker,exec -it sql-pet bash)'
cli[3] <- 'docker exec -ti sql-pet psql -a \n-p 5432 -d dvdrental -U postgres\n\nsystem2(docker,exec -ti sql-pet psql -a \n-p 5432 -d dvdrental -U postgres)'
# R Information
r_lbl <- names(R.Version())[1:7]
r_ver <- R.Version()[1:7]
# RStudio Information
rstudio_lbl <- c('RStudio version','Current program mode')
rstudio_ver <- c(as.character(rstudioapi::versionInfo()$version),rstudioapi::versionInfo()$mode)
# Docker Information
docker_lbl <- c('client version','server version')
docker_ver <- system2("docker", "version", stdout = TRUE) %>%
grep(x = ., pattern = 'Version',value = TRUE) %>%
gsub(x = ., pattern = ' +Version: +', replacement = '')
# Linux Information
linux_lbl <- 'Linux Version'
linux_ver <- system2('docker', 'exec -i sql-pet /bin/uname -r', stdout = TRUE)
# PostgreSQL Information
con <- sp_get_postgres_connection(user = Sys.getenv("DEFAULT_POSTGRES_USER_NAME"),
password = Sys.getenv("DEFAULT_POSTGRES_PASSWORD"),
dbname = "dvdrental",
seconds_to_test = 30)
postgres_ver <- dbGetQuery(con,"select version()") %>%
gsub(x = ., pattern = '\\(.*$', replacement = '')
```
The following code block uses the data generated from the previous code block as input to the subgraphs, the ones outlined in red. The application nodes are the parents of the subgraphs and are not outlined in red. The `Environment` application node represents the machine you are running the tutorial on and hosts the sub-applications.
Note that the '@@' variables are populated at the end of the `Environment` definition following the `## @@1 - @@5` source data comment.
```{r eval=FALSE, echo=TRUE}
grViz("
digraph Envgraph {
# graph, node, and edge definitions
graph [compound = true, nodesep = .5, ranksep = .25,
color = red]
node [fontname = Helvetica, fontcolor = darkslategray,
shape = rectangle, fixedsize = true, width = 1,
color = darkslategray]
edge [color = grey, arrowhead = none, arrowtail = none]
# subgraph for Environment information
subgraph cluster1 {
node [fixedsize = true, width = 3]
'@@1-1'
}
# subgraph for R information
subgraph cluster2 {
node [fixedsize = true, width = 3]
'@@2-1' -> '@@2-2' -> '@@2-3' -> '@@2-4'
'@@2-4' -> '@@2-5' -> '@@2-6' -> '@@2-7'
}
# subgraph for RStudio information
subgraph cluster3 {
node [fixedsize = true, width = 3]
'@@3-1' -> '@@3-2'
}
# subgraph for Docker information
subgraph cluster4 {
node [fixedsize = true, width = 3]
'@@4-1' -> '@@4-2'
}
# subgraph for Docker-Linux information
subgraph cluster5 {
node [fixedsize = true, width = 3]
'@@5-1'
}
# subgraph for Docker-PostgreSQL information
subgraph cluster6 {
node [fixedsize = true, width = 3]
'@@6-1'
}
# subgraph for Docker-PostgreSQL information
subgraph cluster7 {
node [fixedsize = true, height = 1.25, width = 4.0]
'@@7-1' -> '@@7-2' -> '@@7-3'
}
CLI [label='CLI\nRStudio system2',height = .75,width=3.0, color = 'blue' ]
Environment [label = 'Linux,Mac,Windows',width = 2.5]
Environment -> R
Environment -> RStudio
Environment -> Docker
Environment -> '@@1' [lhead = cluster1] # Environment Information
R -> '@@2-1' [lhead = cluster2] # R Information
RStudio -> '@@3' [lhead = cluster3] # RStudio Information
Docker -> '@@4' [lhead = cluster4] # Docker Information
Docker -> '@@5' [lhead = cluster5] # Docker-Linux Information
Docker -> '@@6' [lhead = cluster6] # Docker-PostgreSQL Information
'@@1' -> CLI
CLI -> '@@7' [lhead = cluster7] # CLI
'@@7-2' -> '@@5'
'@@7-3' -> '@@6'
}
[1]: paste0(os_lbl, ':\\n', os_ver)
[2]: paste0(r_lbl, ':\\n', r_ver)
[3]: paste0(rstudio_lbl,':\\n', rstudio_ver)
[4]: paste0(docker_lbl, ':\\n', docker_ver)
[5]: paste0(linux_lbl, ':\\n', linux_ver)
[6]: paste0('PostgreSQL:\\n', postgres_ver)
[7]: cli
")
```
One sub-application not shown above is your local console/terminal/CLI application. In the tutorial, fully constructed docker commands are printed out and then executed. If for some reason the executed docker command fails, one can copy and paste it into your local terminal window to see additional error information. Failures seem more prevalent in the Windows environment.
## Communicating with Docker Applications
In this tutorial, the two main ways to interface with the applications in the Docker container are through the CLI or the RStudio `system2` command. The blue box in the diagram above represents these two interfaces.