Skip to content

Commit

Permalink
added mssql running configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mattocchi committed Aug 1, 2023
1 parent 0dfe4f7 commit f1077ad
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 32 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
[![tests](https://github.com/ddev/ddev-addon-template/actions/workflows/tests.yml/badge.svg)](https://github.com/ddev/ddev-addon-template/actions/workflows/tests.yml) ![project is maintained](https://img.shields.io/maintenance/yes/2024.svg)
[![tests](https://github.com/ddev/ddev-db-mssql/actions/workflows/tests.yml/badge.svg)](https://github.com/ddev/ddev-db-mssql/actions/workflows/tests.yml) ![project is maintained](https://img.shields.io/maintenance/yes/2024.svg)

# ddev-addon-template <!-- omit in toc -->
# ddev-db-mssql <!-- omit in toc -->

* [What is ddev-addon-template?](#what-is-ddev-addon-template)
* [What is ddev-db-mssql?](#what-is-ddev-db-mssql)
* [Components of the repository](#components-of-the-repository)
* [Getting started](#getting-started)

## What is ddev-addon-template?
## What is ddev-db-mssql?

This repository is a template for providing [DDEV](https://ddev.readthedocs.io) add-ons and services.

In DDEV addons can be installed from the command line using the `ddev get` command, for example, `ddev get ddev/ddev-addon-template` or `ddev get ddev/ddev-drupal9-solr`.
In DDEV addons can be installed from the command line using the `ddev get` command, for example, `ddev get ddev/ddev-db-mssql` or `ddev get ddev/ddev-drupal9-solr`.

A repository like this one is the way to get started. You can create a new repo from this one by clicking the template button in the top right corner of the page.

![template button](images/template-button.png)

## Thanks to

* this discussion for a good base configuration [How to install the SQL Server PHP drivers in DDEV-Local](https://stackoverflow.com/questions/58086933/how-to-install-the-sql-server-php-drivers-in-ddev-local)
* this discussion for fix of sqlsrv and pdo_sqlsrv in php [Error when installing sqlsrv and pdo_sqlsrv in php8+ apache docker image #1438](https://github.com/microsoft/msphpsql/issues/1438)

## Components of the repository

* The fundamental contents of the add-on service or other component. For example, in this template there is a [docker-compose.addon-template.yaml](docker-compose.addon-template.yaml) file.
* The fundamental contents of the add-on service or other component. For example, in this template there is a [docker-compose.db-mssql.yaml](docker-compose.db-mssql.yaml) file.
* An [install.yaml](install.yaml) file that describes how to install the service or other component.
* A test suite in [test.bats](tests/test.bats) that makes sure the service continues to work as expected.
* [Github actions setup](.github/workflows/tests.yml) so that the tests run automatically when you push to the repository.
Expand All @@ -27,8 +32,8 @@ A repository like this one is the way to get started. You can create a new repo

1. Choose a good descriptive name for your add-on. It should probably start with "ddev-" and include the basic service or functionality. If it's particular to a specific CMS, perhaps `ddev-<CMS>-servicename`.
2. Create the new template repository by using the template button.
3. Globally replace "addon-template" with the name of your add-on.
4. Add the files that need to be added to a ddev project to the repository. For example, you might remove `docker-compose.addon-template.yaml` with the `docker-compose.*.yaml` for your recipe.
3. Globally replace "db-mssql" with the name of your add-on.
4. Add the files that need to be added to a ddev project to the repository. For example, you might remove `docker-compose.db-mssql.yaml` with the `docker-compose.*.yaml` for your recipe.
5. Update the install.yaml to give the necessary instructions for installing the add-on.

* The fundamental line is the `project_files` directive, a list of files to be copied from this repo into the project `.ddev` directory.
Expand Down
16 changes: 0 additions & 16 deletions docker-compose.addon-template.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions docker-compose.db-mssql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ddev-generated
services:
mssql:
container_name: ddev-${DDEV_SITENAME}-db-mssql
# image: busybox:stable
image: mcr.microsoft.com/mssql/server:2019-latest
ports:
- 1433:1433
environment:
# https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-configure-environment-variables?view=sql-server-2017
MSSQL_SA_PASSWORD: "belloQuesto100"
ACCEPT_EULA: "Y"
networks: [default, ddev_default]
restart: "no"
# These labels ensure this service is discoverable by ddev.
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.approot: $DDEV_APPROOT

volumes:
- ".:/mnt/ddev_config"
8 changes: 4 additions & 4 deletions install.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: addon-template
name: db-mssql

# pre_install_actions - list of actions to run before installing the addon.
# Examples would be removing an extraneous docker volume,
Expand Down Expand Up @@ -56,8 +56,8 @@ pre_install_actions:
# if it hasn't been modified by the user.
# DDEV environment variables can be interpolated into these filenames
project_files:
- docker-compose.addon-template.yaml
# - extra_files/
- docker-compose.db-mssql.yaml
- web-build/
# - somefile.sh

# List of files and directories that are copied into the global .ddev directory
Expand All @@ -70,7 +70,7 @@ global_files:
post_install_actions:
# - chmod +x ~/.ddev/commands/web/somecommand
# - touch somefile.${GOOS}.${DDEV_WEBSERVER}
# - perl -pi -e 's/oldstring/newstring/g' docker-compose.addon-template.yaml
# - perl -pi -e 's/oldstring/newstring/g' docker-compose.db-mssql.yaml

# Advanced usage - yaml files can be read in and then used as go template actions
# in pre_install_actions and post_install_actions
Expand Down
8 changes: 4 additions & 4 deletions tests/test.bats
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
setup() {
set -eu -o pipefail
export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.."
export TESTDIR=~/tmp/test-addon-template
export TESTDIR=~/tmp/test-db-mssql
mkdir -p $TESTDIR
export PROJNAME=test-addon-template
export PROJNAME=test-db-mssql
export DDEV_NON_INTERACTIVE=true
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1 || true
cd "${TESTDIR}"
Expand Down Expand Up @@ -36,8 +36,8 @@ teardown() {
@test "install from release" {
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
echo "# ddev get ddev/ddev-addon-template with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ddev/ddev-addon-template
echo "# ddev get ddev/ddev-db-mssql with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ddev/ddev-db-mssql
ddev restart >/dev/null
health_checks
}
Expand Down
5 changes: 5 additions & 0 deletions web-build/Dockerfile.mssql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ddev-generated
COPY install_sqlsrv.sh /tmp/

RUN apt-get update && DEBIAN_FRONTEND=noninteractive sudo apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests gcc make autoconf libc-dev pkg-config php-pear
RUN sudo /bin/bash /tmp/install_sqlsrv.sh
79 changes: 79 additions & 0 deletions web-build/install_sqlsrv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash
#ddev-generated
# This script installs eMSSQL server. Contains some ddev-specific twaks:
# - doesn't add ondrej's repo because that's already added
# - doesn't source .bashrc because that will happen anyway.
# - doesn't restart Apache; it's not started at this point.
# - assumes it's being run under sudo anyway and doesn't use sudo or su or exit
# - tries to run apt update as few times as possible
# - Apache's mpm_event module is already disabled.
# Those don't work well with Docker builds.

# Optional: Exit if already installed.
if php -m | grep sqlsrv; then
exit
fi

# https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-2017#step-1-install-php-2
# Some of these packages will be redundant.
export DEBIAN_FRONTEND=noninteractive
# Install sqlsrv drivers.
export PHP_VERSIONS="php7.0 php7.1 php7.2 php7.3 php7.4 php8.0 php8.1"
# Note: Only works for PHP 7.0+.
export PHP_SUFFIXES="7.0 7.1 7.2 7.3 7.4 8.0 8.1"

# https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017
curl https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/microsoft.gpg
# Download appropriate package for the OS version
OS=$(grep -P '(?<=^ID=)' /etc/os-release | cut -c 4-)
VERSION=$(lsb_release -rs)

sudo touch /etc/apt/sources.list.d/mssql-release.list
sudo chmod 666 /etc/apt/sources.list.d/mssql-release.list
sudo curl https://packages.microsoft.com/config/$OS/$VERSION/prod.list >/etc/apt/sources.list.d/mssql-release.list
sudo chmod 644 /etc/apt/sources.list.d/mssql-release.list

apt-get update
apt-get install -y curl apt-transport-https
for v in $PHP_VERSIONS; do
apt-get install -y -o Dpkg::Options::="--force-confold" "$v" "$v"-dev "$v"-xml
done
ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >>~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >>~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev
# optional: kerberos library for debian-slim distributions
# sudo apt-get install -y libgssapi-krb5-2

# https://github.com/microsoft/msphpsql/issues/1438
apt-get install -y --allow-downgrades odbcinst=2.3.7 odbcinst1debian2=2.3.7 unixodbc=2.3.7 unixodbc-dev=2.3.7

# https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-2017#step-3-install-the-php-drivers-for-microsoft-sql-server
# See https://stackoverflow.com/questions/40419718/how-to-install-php-extension-using-pecl-for-specific-php-version-when-several-p/48352487
for v in $PHP_SUFFIXES; do
pecl -d php_suffix="$v" install sqlsrv
pecl -d php_suffix="$v" install pdo_sqlsrv
# This does not remove the extensions; it just removes the metadata that says
# the extensions are installed.
pecl uninstall -r sqlsrv
pecl uninstall -r pdo_sqlsrv
done
for v in $PHP_SUFFIXES; do
touch /etc/php/"$v"/mods-available/sqlsrv.ini
touch /etc/php/"$v"/mods-available/pdo_sqlsrv.ini
chmod 666 /etc/php/"$v"/mods-available/*sqlsrv*.ini
printf "; priority=20\nextension=sqlsrv.so\n" >/etc/php/"$v"/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" >/etc/php/"$v"/mods-available/pdo_sqlsrv.ini
done
phpenmod sqlsrv pdo_sqlsrv

# Step 4 skipped because Apache is already configured.
# Step 5 skipped because Apache is not started at this point.

# Reduce image size some.
if [ -f "/.dockerenv" ]; then
rm -rf /var/lib/apt/lists/*
fi

0 comments on commit f1077ad

Please sign in to comment.